From 81826a2da9ff43f8a0257ff1bd3af870dc694e16 Mon Sep 17 00:00:00 2001 From: Zintle Skosana Date: Tue, 22 Jan 2019 14:29:17 +0200 Subject: [PATCH 1/3] Migration --- ...190122091301_InitialMigrations.Designer.cs | 57 +++++++++++++++++++ .../20190122091301_InitialMigrations.cs | 40 +++++++++++++ .../TimesheetContextModelSnapshot.cs | 55 ++++++++++++++++++ .../MyTimesheet/MyTimesheet.csproj | 1 + src/MyTimesheet/MyTimesheet/Startup.cs | 2 +- 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/MyTimesheet/MyTimesheet/Migrations/20190122091301_InitialMigrations.Designer.cs create mode 100644 src/MyTimesheet/MyTimesheet/Migrations/20190122091301_InitialMigrations.cs create mode 100644 src/MyTimesheet/MyTimesheet/Migrations/TimesheetContextModelSnapshot.cs diff --git a/src/MyTimesheet/MyTimesheet/Migrations/20190122091301_InitialMigrations.Designer.cs b/src/MyTimesheet/MyTimesheet/Migrations/20190122091301_InitialMigrations.Designer.cs new file mode 100644 index 0000000..d9fe875 --- /dev/null +++ b/src/MyTimesheet/MyTimesheet/Migrations/20190122091301_InitialMigrations.Designer.cs @@ -0,0 +1,57 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using MyTimesheet.Models; + +namespace MyTimesheet.Migrations +{ + [DbContext(typeof(TimesheetContext))] + [Migration("20190122091301_InitialMigrations")] + partial class InitialMigrations + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "2.2.1-servicing-10028") + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("MyTimesheet.Models.TimesheetEntry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("Billable"); + + b.Property("Client"); + + b.Property("Date"); + + b.Property("Description"); + + b.Property("Duration"); + + b.Property("Name"); + + b.Property("Project"); + + b.Property("Surname"); + + b.Property("TimeEnd"); + + b.Property("TimeStart"); + + b.HasKey("Id"); + + b.ToTable("Entries"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/MyTimesheet/MyTimesheet/Migrations/20190122091301_InitialMigrations.cs b/src/MyTimesheet/MyTimesheet/Migrations/20190122091301_InitialMigrations.cs new file mode 100644 index 0000000..e28b4a5 --- /dev/null +++ b/src/MyTimesheet/MyTimesheet/Migrations/20190122091301_InitialMigrations.cs @@ -0,0 +1,40 @@ +using System; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace MyTimesheet.Migrations +{ + public partial class InitialMigrations : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Entries", + columns: table => new + { + Id = table.Column(nullable: false) + .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), + Name = table.Column(nullable: true), + Surname = table.Column(nullable: true), + Client = table.Column(nullable: true), + Project = table.Column(nullable: true), + Date = table.Column(nullable: false), + TimeStart = table.Column(nullable: false), + TimeEnd = table.Column(nullable: false), + Duration = table.Column(nullable: false), + Description = table.Column(nullable: true), + Billable = table.Column(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Entries", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Entries"); + } + } +} diff --git a/src/MyTimesheet/MyTimesheet/Migrations/TimesheetContextModelSnapshot.cs b/src/MyTimesheet/MyTimesheet/Migrations/TimesheetContextModelSnapshot.cs new file mode 100644 index 0000000..6404921 --- /dev/null +++ b/src/MyTimesheet/MyTimesheet/Migrations/TimesheetContextModelSnapshot.cs @@ -0,0 +1,55 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using MyTimesheet.Models; + +namespace MyTimesheet.Migrations +{ + [DbContext(typeof(TimesheetContext))] + partial class TimesheetContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "2.2.1-servicing-10028") + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("MyTimesheet.Models.TimesheetEntry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("Billable"); + + b.Property("Client"); + + b.Property("Date"); + + b.Property("Description"); + + b.Property("Duration"); + + b.Property("Name"); + + b.Property("Project"); + + b.Property("Surname"); + + b.Property("TimeEnd"); + + b.Property("TimeStart"); + + b.HasKey("Id"); + + b.ToTable("Entries"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/MyTimesheet/MyTimesheet/MyTimesheet.csproj b/src/MyTimesheet/MyTimesheet/MyTimesheet.csproj index 54dfccf..a986b6b 100644 --- a/src/MyTimesheet/MyTimesheet/MyTimesheet.csproj +++ b/src/MyTimesheet/MyTimesheet/MyTimesheet.csproj @@ -9,6 +9,7 @@ + diff --git a/src/MyTimesheet/MyTimesheet/Startup.cs b/src/MyTimesheet/MyTimesheet/Startup.cs index e81072d..181cd12 100644 --- a/src/MyTimesheet/MyTimesheet/Startup.cs +++ b/src/MyTimesheet/MyTimesheet/Startup.cs @@ -35,7 +35,7 @@ public void ConfigureServices(IServiceCollection services) c.SwaggerDoc("v1", new Info { Title = "My Timesheet API", Version = "v1" }); }); - var connection = @"Server=sql101labs1793591179000.westeurope.cloudapp.azure.com;Database=sql101.#NAME.SURNAME;User Id=myUsername;Password=myPassword;"; + var connection = @"Server=sql101labs1793591179000.westeurope.cloudapp.azure.com;Database=sql101.Zintle.Skosana;User Id=ZintleSkosana;Password=35983076zsS#;"; services.AddDbContext (options => options.UseSqlServer(connection)); } From d148a06aa8a4764578bca2389c4f513dfe582b73 Mon Sep 17 00:00:00 2001 From: Zintle Skosana Date: Sat, 26 Jan 2019 16:43:26 +0200 Subject: [PATCH 2/3] Created SessionController for new table after normalization --- .../Controllers/SessionController.cs | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/MyTimesheet/MyTimesheet/Controllers/SessionController.cs diff --git a/src/MyTimesheet/MyTimesheet/Controllers/SessionController.cs b/src/MyTimesheet/MyTimesheet/Controllers/SessionController.cs new file mode 100644 index 0000000..c7c4314 --- /dev/null +++ b/src/MyTimesheet/MyTimesheet/Controllers/SessionController.cs @@ -0,0 +1,80 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using MyTimesheet.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Configuration; +using StackExchange.Redis; +using Microsoft.Extensions.Configuration; + +namespace MyTimesheet.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class SessionController : ControllerBase + { + private readonly TimesheetContext _db; + private readonly IConfiguration _configuration; + public SessionController(TimesheetContext context, IConfiguration config) + { + _db = context; + _configuration = config; + } + + // GET api/values + [HttpGet] + public async Task>> Get() + { + return await _db.Sessions.ToListAsync(); + } + + // GET api/values/5 + [HttpGet("{Session_Id}")] + public async Task> Get(int id) + { + return await _db.Sessions.FindAsync(id); + } + + // POST api/values + [HttpPost] + public async Task Post([FromBody] Session value) + { + await _db.Sessions.AddAsync(value); + await _db.SaveChangesAsync(); + + var cacheConnection = _configuration.GetValue("CacheConnection").ToString(); + var lazyConnection = new Lazy(() => + { + + return ConnectionMultiplexer.Connect(cacheConnection); + }); + IDatabase cache = lazyConnection.Value.GetDatabase(); + await cache.SetAddAsync($"{value.Date} - {value.TimeStart} - {value.TimeEnd}", value.ToString()); + var cacheItem = await cache.StringGetAsync($"{value.Date} - {value.TimeStart} - {value.TimeEnd}"); + + lazyConnection.Value.Dispose(); + return cacheItem; + } + + + // PUT api/values/5 + [HttpPut("{id}")] + public async Task Put(int id, [FromBody] Session value) + { + var entry = await _db.Sessions.FindAsync(id); + entry = value; + await _db.SaveChangesAsync(); + } + + // DELETE api/values/5 + [HttpDelete("{id}")] + public async Task Delete(int id) + { + var entry = await _db.Sessions.FindAsync(id); + _db.Sessions.Remove(entry); + await _db.SaveChangesAsync(); + } + } +} From 41c23b5a24fc87a2855c1e1169f19452ad26ea2d Mon Sep 17 00:00:00 2001 From: Zintle Skosana Date: Sat, 26 Jan 2019 16:53:12 +0200 Subject: [PATCH 3/3] Updated SessionController --- .../Controllers/SessionController.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/MyTimesheet/MyTimesheet/Controllers/SessionController.cs b/src/MyTimesheet/MyTimesheet/Controllers/SessionController.cs index c7c4314..907f9df 100644 --- a/src/MyTimesheet/MyTimesheet/Controllers/SessionController.cs +++ b/src/MyTimesheet/MyTimesheet/Controllers/SessionController.cs @@ -15,9 +15,9 @@ namespace MyTimesheet.Controllers [ApiController] public class SessionController : ControllerBase { - private readonly TimesheetContext _db; + private readonly SessionContext _db; private readonly IConfiguration _configuration; - public SessionController(TimesheetContext context, IConfiguration config) + public SessionController(SessionContext context, IConfiguration config) { _db = context; _configuration = config; @@ -25,23 +25,23 @@ public SessionController(TimesheetContext context, IConfiguration config) // GET api/values [HttpGet] - public async Task>> Get() + public async Task>> Get() { - return await _db.Sessions.ToListAsync(); + return await _db.Entries.ToListAsync(); } // GET api/values/5 [HttpGet("{Session_Id}")] - public async Task> Get(int id) + public async Task> Get(int id) { - return await _db.Sessions.FindAsync(id); + return await _db.Entries.FindAsync(id); } // POST api/values [HttpPost] - public async Task Post([FromBody] Session value) + public async Task Post([FromBody] SessionEntry value) { - await _db.Sessions.AddAsync(value); + await _db.Entries.AddAsync(value); await _db.SaveChangesAsync(); var cacheConnection = _configuration.GetValue("CacheConnection").ToString(); @@ -61,9 +61,9 @@ public async Task Post([FromBody] Session value) // PUT api/values/5 [HttpPut("{id}")] - public async Task Put(int id, [FromBody] Session value) + public async Task Put(int id, [FromBody] SessionEntry value) { - var entry = await _db.Sessions.FindAsync(id); + var entry = await _db.Entries.FindAsync(id); entry = value; await _db.SaveChangesAsync(); }