From 1da607463d5c5e7b7ad3a8fb4196744d2b09e480 Mon Sep 17 00:00:00 2001 From: dadamsretrorabbit Date: Tue, 22 Jan 2019 09:43:24 +0200 Subject: [PATCH 1/5] Changes details for Database and User --- data/createdatabase.sql | 74 +++++++++++++------------- data/createuser.sql | 12 ++--- src/MyTimesheet/MyTimesheet/Startup.cs | 2 +- 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/data/createdatabase.sql b/data/createdatabase.sql index 41b5a17..fb3fddc 100644 --- a/data/createdatabase.sql +++ b/data/createdatabase.sql @@ -2,114 +2,114 @@ USE [master] GO /****** Object: Database [sql101.firstname.lastname] Script Date: 1/21/2019 9:11:50 PM ******/ -CREATE DATABASE [sql101.firstname.lastname] +CREATE DATABASE [sql101.darren.adams] CONTAINMENT = NONE ON PRIMARY -( NAME = N'sql101.firstname.lastname', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\sql101.firstname.lastname.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB ) +( NAME = N'sql101.darren.adams', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\sql101.darren.adams.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB ) LOG ON -( NAME = N'sql101.firstname.lastname_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\sql101.firstname.lastname_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB ) +( NAME = N'sql101.darren.adams', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\sql101.darren.adams_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB ) GO -ALTER DATABASE [sql101.firstname.lastname] SET COMPATIBILITY_LEVEL = 130 +ALTER DATABASE [sql101.darren.adams] SET COMPATIBILITY_LEVEL = 130 GO IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) begin -EXEC [sql101.firstname.lastname].[dbo].[sp_fulltext_database] @action = 'enable' +EXEC [sql101.darren.adams].[dbo].[sp_fulltext_database] @action = 'enable' end GO -ALTER DATABASE [sql101.firstname.lastname] SET ANSI_NULL_DEFAULT OFF +ALTER DATABASE [sql101.darren.adams] SET ANSI_NULL_DEFAULT OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET ANSI_NULLS OFF +ALTER DATABASE [sql101.darren.adams] SET ANSI_NULLS OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET ANSI_PADDING OFF +ALTER DATABASE [sql101.darren.adams] SET ANSI_PADDING OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET ANSI_WARNINGS OFF +ALTER DATABASE [sql101.darren.adams] SET ANSI_WARNINGS OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET ARITHABORT OFF +ALTER DATABASE [sql101.darren.adams] SET ARITHABORT OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET AUTO_CLOSE OFF +ALTER DATABASE [sql101.darren.adams] SET AUTO_CLOSE OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET AUTO_SHRINK OFF +ALTER DATABASE [sql101.darren.adams] SET AUTO_SHRINK OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET AUTO_UPDATE_STATISTICS ON +ALTER DATABASE [sql101.darren.adams] SET AUTO_UPDATE_STATISTICS ON GO -ALTER DATABASE [sql101.firstname.lastname] SET CURSOR_CLOSE_ON_COMMIT OFF +ALTER DATABASE [sql101.darren.adams] SET CURSOR_CLOSE_ON_COMMIT OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET CURSOR_DEFAULT GLOBAL +ALTER DATABASE [sql101.darren.adams] SET CURSOR_DEFAULT GLOBAL GO -ALTER DATABASE [sql101.firstname.lastname] SET CONCAT_NULL_YIELDS_NULL OFF +ALTER DATABASE [sql101.darren.adams] SET CONCAT_NULL_YIELDS_NULL OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET NUMERIC_ROUNDABORT OFF +ALTER DATABASE [sql101.darren.adams] SET NUMERIC_ROUNDABORT OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET QUOTED_IDENTIFIER OFF +ALTER DATABASE [sql101.darren.adams] SET QUOTED_IDENTIFIER OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET RECURSIVE_TRIGGERS OFF +ALTER DATABASE [sql101.darren.adams] SET RECURSIVE_TRIGGERS OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET DISABLE_BROKER +ALTER DATABASE [sql101.darren.adams] SET DISABLE_BROKER GO -ALTER DATABASE [sql101.firstname.lastname] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +ALTER DATABASE [sql101.darren.adams] SET AUTO_UPDATE_STATISTICS_ASYNC OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET DATE_CORRELATION_OPTIMIZATION OFF +ALTER DATABASE [sql101.darren.adams] SET DATE_CORRELATION_OPTIMIZATION OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET TRUSTWORTHY OFF +ALTER DATABASE [sql101.darren.adams] SET TRUSTWORTHY OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET ALLOW_SNAPSHOT_ISOLATION OFF +ALTER DATABASE [sql101.darren.adams] SET ALLOW_SNAPSHOT_ISOLATION OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET PARAMETERIZATION SIMPLE +ALTER DATABASE [sql101.darren.adams] SET PARAMETERIZATION SIMPLE GO -ALTER DATABASE [sql101.firstname.lastname] SET READ_COMMITTED_SNAPSHOT OFF +ALTER DATABASE [sql101.darren.adams] SET READ_COMMITTED_SNAPSHOT OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET HONOR_BROKER_PRIORITY OFF +ALTER DATABASE [sql101.darren.adams] SET HONOR_BROKER_PRIORITY OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET RECOVERY FULL +ALTER DATABASE [sql101.darren.adams] SET RECOVERY FULL GO -ALTER DATABASE [sql101.firstname.lastname] SET MULTI_USER +ALTER DATABASE [sql101.darren.adams] SET MULTI_USER GO -ALTER DATABASE [sql101.firstname.lastname] SET PAGE_VERIFY CHECKSUM +ALTER DATABASE [sql101.darren.adams] SET PAGE_VERIFY CHECKSUM GO -ALTER DATABASE [sql101.firstname.lastname] SET DB_CHAINING OFF +ALTER DATABASE [sql101.darren.adams] SET DB_CHAINING OFF GO -ALTER DATABASE [sql101.firstname.lastname] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +ALTER DATABASE [sql101.darren.adams] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) GO -ALTER DATABASE [sql101.firstname.lastname] SET TARGET_RECOVERY_TIME = 60 SECONDS +ALTER DATABASE [sql101.darren.adams] SET TARGET_RECOVERY_TIME = 60 SECONDS GO -ALTER DATABASE [sql101.firstname.lastname] SET DELAYED_DURABILITY = DISABLED +ALTER DATABASE [sql101.darren.adams] SET DELAYED_DURABILITY = DISABLED GO -ALTER DATABASE [sql101.firstname.lastname] SET QUERY_STORE = OFF +ALTER DATABASE [sql101.darren.adams] SET QUERY_STORE = OFF GO -USE [sql101.firstname.lastname] +USE [sql101.darren.adams] GO ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = OFF; @@ -124,7 +124,7 @@ GO ALTER DATABASE SCOPED CONFIGURATION SET QUERY_OPTIMIZER_HOTFIXES = OFF; GO -ALTER DATABASE [sql101.firstname.lastname] SET READ_WRITE +ALTER DATABASE [sql101.darren.adams] SET READ_WRITE GO diff --git a/data/createuser.sql b/data/createuser.sql index bcf3038..feb843c 100644 --- a/data/createuser.sql +++ b/data/createuser.sql @@ -1,18 +1,18 @@ USE [master] GO -CREATE LOGIN [firstnamelastname] WITH PASSWORD=N'rabbit123!@#' MUST_CHANGE, DEFAULT_DATABASE=[sql101.firstname.lastname], CHECK_EXPIRATION=ON, CHECK_POLICY=ON +CREATE LOGIN [darrenadams] WITH PASSWORD=N'rabbit123!@#' MUST_CHANGE, DEFAULT_DATABASE=[sql101.darren.adams], CHECK_EXPIRATION=ON, CHECK_POLICY=ON GO -use [sql101.firstname.lastname] +use [sql101.darren.adams] GO use [master] GO -USE [sql101.firstname.lastname] +USE [sql101.darren.adams] GO -CREATE USER [firstnamelastname] FOR LOGIN [firstnamelastname] +CREATE USER [darrenadams] FOR LOGIN [darrenadams] GO -USE [sql101.firstname.lastname] +USE [sql101.darren.adams] GO -ALTER ROLE [db_owner] ADD MEMBER [firstnamelastname] +ALTER ROLE [db_owner] ADD MEMBER [darrenadams] GO diff --git a/src/MyTimesheet/MyTimesheet/Startup.cs b/src/MyTimesheet/MyTimesheet/Startup.cs index e81072d..1083c7b 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.darren.adams;User Id=darrenadams;Password=N'rabbit123!@#';"; services.AddDbContext (options => options.UseSqlServer(connection)); } From 8f6df9768302371c7ad469bcd03a451d23e08d3a Mon Sep 17 00:00:00 2001 From: dadamsretrorabbit Date: Tue, 22 Jan 2019 14:26:55 +0200 Subject: [PATCH 2/5] Adds Initial Migration --- ...0190122091040_InitialMigration.Designer.cs | 57 +++++++++++++++++++ .../20190122091040_InitialMigration.cs | 40 +++++++++++++ .../TimesheetContextModelSnapshot.cs | 55 ++++++++++++++++++ .../MyTimesheet/MyTimesheet.csproj | 1 + src/MyTimesheet/MyTimesheet/Startup.cs | 3 +- 5 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 src/MyTimesheet/MyTimesheet/Migrations/20190122091040_InitialMigration.Designer.cs create mode 100644 src/MyTimesheet/MyTimesheet/Migrations/20190122091040_InitialMigration.cs create mode 100644 src/MyTimesheet/MyTimesheet/Migrations/TimesheetContextModelSnapshot.cs diff --git a/src/MyTimesheet/MyTimesheet/Migrations/20190122091040_InitialMigration.Designer.cs b/src/MyTimesheet/MyTimesheet/Migrations/20190122091040_InitialMigration.Designer.cs new file mode 100644 index 0000000..3d1e678 --- /dev/null +++ b/src/MyTimesheet/MyTimesheet/Migrations/20190122091040_InitialMigration.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("20190122091040_InitialMigration")] + partial class InitialMigration + { + 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/20190122091040_InitialMigration.cs b/src/MyTimesheet/MyTimesheet/Migrations/20190122091040_InitialMigration.cs new file mode 100644 index 0000000..bd41006 --- /dev/null +++ b/src/MyTimesheet/MyTimesheet/Migrations/20190122091040_InitialMigration.cs @@ -0,0 +1,40 @@ +using System; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace MyTimesheet.Migrations +{ + public partial class InitialMigration : 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..9a45ae8 100644 --- a/src/MyTimesheet/MyTimesheet/MyTimesheet.csproj +++ b/src/MyTimesheet/MyTimesheet/MyTimesheet.csproj @@ -3,6 +3,7 @@ netcoreapp2.2 InProcess + MyTimesheet.Program diff --git a/src/MyTimesheet/MyTimesheet/Startup.cs b/src/MyTimesheet/MyTimesheet/Startup.cs index 1083c7b..7db6151 100644 --- a/src/MyTimesheet/MyTimesheet/Startup.cs +++ b/src/MyTimesheet/MyTimesheet/Startup.cs @@ -35,7 +35,8 @@ 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.darren.adams;User Id=darrenadams;Password=N'rabbit123!@#';"; + var connection = @"Server=sql101labs1793591179000.westeurope.cloudapp.azure.com;Database=sql101.darren.adams;User Id=darrenadams;Password=Darren@1994;"; + services.AddDbContext (options => options.UseSqlServer(connection)); } From f8e42b064fa26e9488feaef14a68c385c16f070e Mon Sep 17 00:00:00 2001 From: dadamsretrorabbit Date: Tue, 22 Jan 2019 16:14:25 +0200 Subject: [PATCH 3/5] Adds Redis for Post Adds Redis to project --- CacheSecrets.config | 4 +++ .../MyTimesheet/CacheSecrets.config | 3 ++ .../Controllers/TimesheetController.cs | 36 +++++++++++++++++-- src/MyTimesheet/MyTimesheet/Program.cs | 1 + src/MyTimesheet/MyTimesheet/Startup.cs | 1 + src/MyTimesheet/MyTimesheet/appsettings.json | 13 +++---- 6 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 CacheSecrets.config create mode 100644 src/MyTimesheet/MyTimesheet/CacheSecrets.config diff --git a/CacheSecrets.config b/CacheSecrets.config new file mode 100644 index 0000000..d2060e3 --- /dev/null +++ b/CacheSecrets.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/MyTimesheet/MyTimesheet/CacheSecrets.config b/src/MyTimesheet/MyTimesheet/CacheSecrets.config new file mode 100644 index 0000000..f375875 --- /dev/null +++ b/src/MyTimesheet/MyTimesheet/CacheSecrets.config @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/src/MyTimesheet/MyTimesheet/Controllers/TimesheetController.cs b/src/MyTimesheet/MyTimesheet/Controllers/TimesheetController.cs index b2f5a7c..08d6e37 100644 --- a/src/MyTimesheet/MyTimesheet/Controllers/TimesheetController.cs +++ b/src/MyTimesheet/MyTimesheet/Controllers/TimesheetController.cs @@ -5,6 +5,10 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using StackExchange.Redis; +using System.Configuration; +using Microsoft.Extensions.Configuration; +using Newtonsoft.Json; namespace MyTimesheet.Controllers { @@ -13,16 +17,20 @@ namespace MyTimesheet.Controllers public class TimesheetController : ControllerBase { private readonly TimesheetContext _db; - public TimesheetController(TimesheetContext context) + readonly IConfiguration _config; + public TimesheetController(TimesheetContext context,IConfiguration config) { _db = context; + _config = config; } // GET api/values [HttpGet] public async Task>> Get() { + return await _db.Entries.ToListAsync(); + } // GET api/values/5 @@ -30,14 +38,38 @@ public async Task>> Get() public async Task> Get(int id) { return await _db.Entries.FindAsync(id); + + } // POST api/values [HttpPost] - public async Task Post([FromBody] TimesheetEntry value) + public async Task Post([FromBody] TimesheetEntry value) { + await _db.Entries.AddAsync(value); await _db.SaveChangesAsync(); + + + + + var lazyConnection = new Lazy(() => + { + //var cacheConnection = _config.GetValue("CacheConnection").ToString(); + return ConnectionMultiplexer.Connect(_config.GetValue("CacheConnection").ToString()); + }); + + IDatabase cache = lazyConnection.Value.GetDatabase(); + var serializeddata = JsonConvert.SerializeObject(value); + await cache.StringSetAsync($"{value.Name}--{value.Surname}", serializeddata); + + var cacheItem = await cache.StringGetAsync($"{value.Name}--{value.Surname}"); + lazyConnection.Value.Dispose(); + + return cacheItem; + + + } // PUT api/values/5 diff --git a/src/MyTimesheet/MyTimesheet/Program.cs b/src/MyTimesheet/MyTimesheet/Program.cs index c0c9bbf..e7421bf 100644 --- a/src/MyTimesheet/MyTimesheet/Program.cs +++ b/src/MyTimesheet/MyTimesheet/Program.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; + namespace MyTimesheet { public class Program diff --git a/src/MyTimesheet/MyTimesheet/Startup.cs b/src/MyTimesheet/MyTimesheet/Startup.cs index 7db6151..4e6cab3 100644 --- a/src/MyTimesheet/MyTimesheet/Startup.cs +++ b/src/MyTimesheet/MyTimesheet/Startup.cs @@ -39,6 +39,7 @@ public void ConfigureServices(IServiceCollection services) services.AddDbContext (options => options.UseSqlServer(connection)); + services.AddSingleton(Configuration); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/src/MyTimesheet/MyTimesheet/appsettings.json b/src/MyTimesheet/MyTimesheet/appsettings.json index def9159..438f796 100644 --- a/src/MyTimesheet/MyTimesheet/appsettings.json +++ b/src/MyTimesheet/MyTimesheet/appsettings.json @@ -1,8 +1,9 @@ { - "Logging": { - "LogLevel": { - "Default": "Warning" - } - }, - "AllowedHosts": "*" + "Logging": { + "LogLevel": { + "Default": "Warning" + } + }, + "AllowedHosts": "*", + "CacheConnection": "101.redis.cache.windows.net,abortConnect=false,ssl=true,password=85BYRVqUHV6aW7Jz8zIIxRnaYMgbBd2kwo4bpDiz4vw=" } From 47e57d7d35ef00bd00b0144d42f6cc81ac21b51a Mon Sep 17 00:00:00 2001 From: dadamsretrorabbit Date: Fri, 25 Jan 2019 20:11:50 +0200 Subject: [PATCH 4/5] Modifies Database to Attempt 3NF --- .../MyTimesheet/Models/ClientContext.cs | 18 ++++++++++++++++++ .../MyTimesheet/Models/ClientEntry.cs | 13 +++++++++++++ .../MyTimesheet/Models/EmployeeContext.cs | 18 ++++++++++++++++++ .../MyTimesheet/Models/EmployeeEntry.cs | 15 +++++++++++++++ .../MyTimesheet/Models/TimesheetEntry.cs | 11 ++++++++--- 5 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 src/MyTimesheet/MyTimesheet/Models/ClientContext.cs create mode 100644 src/MyTimesheet/MyTimesheet/Models/ClientEntry.cs create mode 100644 src/MyTimesheet/MyTimesheet/Models/EmployeeContext.cs create mode 100644 src/MyTimesheet/MyTimesheet/Models/EmployeeEntry.cs diff --git a/src/MyTimesheet/MyTimesheet/Models/ClientContext.cs b/src/MyTimesheet/MyTimesheet/Models/ClientContext.cs new file mode 100644 index 0000000..7ab1812 --- /dev/null +++ b/src/MyTimesheet/MyTimesheet/Models/ClientContext.cs @@ -0,0 +1,18 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyTimesheet.Models +{ + public class ClientContext : DbContext + { + public ClientContext(DbContextOptions options) + : base(options) + { } + + public DbSet Entries { get; set; } + + } +} \ No newline at end of file diff --git a/src/MyTimesheet/MyTimesheet/Models/ClientEntry.cs b/src/MyTimesheet/MyTimesheet/Models/ClientEntry.cs new file mode 100644 index 0000000..3323e74 --- /dev/null +++ b/src/MyTimesheet/MyTimesheet/Models/ClientEntry.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyTimesheet.Models +{ + public class ClientEntry + { + public int ClientId { get; set; } + public string ClientName { get; set; } + } +} diff --git a/src/MyTimesheet/MyTimesheet/Models/EmployeeContext.cs b/src/MyTimesheet/MyTimesheet/Models/EmployeeContext.cs new file mode 100644 index 0000000..0044a1c --- /dev/null +++ b/src/MyTimesheet/MyTimesheet/Models/EmployeeContext.cs @@ -0,0 +1,18 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyTimesheet.Models +{ + public class EmployeeContext : DbContext + { + public EmployeeContext(DbContextOptions options) + : base(options) + { } + + public DbSet Entries { get; set; } + + } +} \ No newline at end of file diff --git a/src/MyTimesheet/MyTimesheet/Models/EmployeeEntry.cs b/src/MyTimesheet/MyTimesheet/Models/EmployeeEntry.cs new file mode 100644 index 0000000..328b7ae --- /dev/null +++ b/src/MyTimesheet/MyTimesheet/Models/EmployeeEntry.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyTimesheet.Models +{ + public class EmployeeEntry + { + + public int EmployeeId { get; set; } + public string Name { get; set; } + public string Surname { get; set; } + } +} diff --git a/src/MyTimesheet/MyTimesheet/Models/TimesheetEntry.cs b/src/MyTimesheet/MyTimesheet/Models/TimesheetEntry.cs index 8c65667..c170246 100644 --- a/src/MyTimesheet/MyTimesheet/Models/TimesheetEntry.cs +++ b/src/MyTimesheet/MyTimesheet/Models/TimesheetEntry.cs @@ -13,11 +13,16 @@ public class TimesheetEntry John | Doe | Client X | Website | 2019-01-22 | 09:00 | 11:00 | 120 | I was rocking HTML5 | YES John | Doe | Client X | API | 2019-01-22 | 13:00 | 17:00 | 240 | Grafting on golang api | YES */ + /* + * EmployeeID | ClientID | Project | Date | Time Started | Time ended | Duration | Description | Billable + -- | -- | --- | --- | --- | --- | --- | --- | --- + 0 | 0 | Website | 2019-01-22 | 09:00 | 11:00 | 120 | I was rocking HTML5 | YES + 0 | 0 | API | 2019-01-22 | 13:00 | 17:00 | 240 | Grafting on golang api | YES + */ public int Id { get; set; } - public string Name { get; set; } - public string Surname { get; set; } - public string Client { get; set; } + public int EmployeeId { get; set; } + public string ClientId { get; set; } public string Project { get; set; } public DateTime Date { get; set; } public DateTime TimeStart { get; set; } From 559b77a26beb78146a27f031f4a462312f42b7bf Mon Sep 17 00:00:00 2001 From: dadamsretrorabbit Date: Fri, 25 Jan 2019 21:07:14 +0200 Subject: [PATCH 5/5] Fixes Controller Logic Adds Put Logic Into Controller --- .../Controllers/ClientController.cs | 77 ++++++++++++++++++ .../Controllers/EmployeeController.cs | 80 +++++++++++++++++++ .../Controllers/TimesheetController.cs | 33 ++++++-- .../MyTimesheet/Models/TimesheetEntry.cs | 6 ++ 4 files changed, 188 insertions(+), 8 deletions(-) create mode 100644 src/MyTimesheet/MyTimesheet/Controllers/ClientController.cs create mode 100644 src/MyTimesheet/MyTimesheet/Controllers/EmployeeController.cs diff --git a/src/MyTimesheet/MyTimesheet/Controllers/ClientController.cs b/src/MyTimesheet/MyTimesheet/Controllers/ClientController.cs new file mode 100644 index 0000000..e94cf96 --- /dev/null +++ b/src/MyTimesheet/MyTimesheet/Controllers/ClientController.cs @@ -0,0 +1,77 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using MyTimesheet.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using StackExchange.Redis; +using System.Configuration; +using Microsoft.Extensions.Configuration; +using Newtonsoft.Json; + +namespace MyTimesheet.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ClientController : ControllerBase + { + private readonly ClientContext _db; + readonly IConfiguration _config; + public ClientController(ClientContext context, IConfiguration config) + { + _db = context; + _config = config; + } + + // GET api/values + [HttpGet] + public async Task>> Get() + { + + return await _db.Entries.ToListAsync(); + + } + + // GET api/values/5 + [HttpGet("{id}")] + public async Task> Get(int id) + { + return await _db.Entries.FindAsync(id); + + + } + + // POST api/values + [HttpPost] + public async Task Post([FromBody] ClientEntry value) + { + + await _db.Entries.AddAsync(value); + await _db.SaveChangesAsync(); + + return value.ClientName + " - " + value.ClientId; + + + + } + + // PUT api/values/5 + [HttpPut("{id}")] + public async Task Put(int id, [FromBody] ClientEntry value) + { + var entry = await _db.Entries.FindAsync(id); + entry = value; + await _db.SaveChangesAsync(); + } + + // DELETE api/values/5 + [HttpDelete("{id}")] + public async Task Delete(int id) + { + var entry = await _db.Entries.FindAsync(id); + _db.Entries.Remove(entry); + await _db.SaveChangesAsync(); + } + } +} diff --git a/src/MyTimesheet/MyTimesheet/Controllers/EmployeeController.cs b/src/MyTimesheet/MyTimesheet/Controllers/EmployeeController.cs new file mode 100644 index 0000000..f55d041 --- /dev/null +++ b/src/MyTimesheet/MyTimesheet/Controllers/EmployeeController.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 StackExchange.Redis; +using System.Configuration; +using Microsoft.Extensions.Configuration; +using Newtonsoft.Json; + +namespace MyTimesheet.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class EmployeeController : ControllerBase + { + private readonly EmployeeContext _db; + readonly IConfiguration _config; + public EmployeeController(EmployeeContext context, IConfiguration config) + { + _db = context; + _config = config; + } + + // GET api/values + [HttpGet] + public async Task>> Get() + { + + return await _db.Entries.ToListAsync(); + + } + + // GET api/values/5 + [HttpGet("{id}")] + public async Task> Get(int id) + { + return await _db.Entries.FindAsync(id); + + + } + + // POST api/values + [HttpPost] + public async Task Post([FromBody] EmployeeEntry value) + { + + await _db.Entries.AddAsync(value); + await _db.SaveChangesAsync(); + + + + + return value.EmployeeId + " - " + value.Name + " : " + value.Surname; + + + + } + + // PUT api/values/5 + [HttpPut("{id}")] + public async Task Put(int id, [FromBody] EmployeeEntry value) + { + var entry = await _db.Entries.FindAsync(id); + entry = value; + await _db.SaveChangesAsync(); + } + + // DELETE api/values/5 + [HttpDelete("{id}")] + public async Task Delete(int id) + { + var entry = await _db.Entries.FindAsync(id); + _db.Entries.Remove(entry); + await _db.SaveChangesAsync(); + } + } +} diff --git a/src/MyTimesheet/MyTimesheet/Controllers/TimesheetController.cs b/src/MyTimesheet/MyTimesheet/Controllers/TimesheetController.cs index 08d6e37..ae5af5c 100644 --- a/src/MyTimesheet/MyTimesheet/Controllers/TimesheetController.cs +++ b/src/MyTimesheet/MyTimesheet/Controllers/TimesheetController.cs @@ -28,8 +28,10 @@ public TimesheetController(TimesheetContext context,IConfiguration config) [HttpGet] public async Task>> Get() { - - return await _db.Entries.ToListAsync(); + + return await _db.Entries.ToListAsync(); + + } @@ -46,7 +48,7 @@ public async Task> Get(int id) [HttpPost] public async Task Post([FromBody] TimesheetEntry value) { - + await _db.Entries.AddAsync(value); await _db.SaveChangesAsync(); @@ -55,15 +57,15 @@ public async Task Post([FromBody] TimesheetEntry value) var lazyConnection = new Lazy(() => { - //var cacheConnection = _config.GetValue("CacheConnection").ToString(); - return ConnectionMultiplexer.Connect(_config.GetValue("CacheConnection").ToString()); + + return ConnectionMultiplexer.Connect(_config.GetValue("CacheConnection")); }); IDatabase cache = lazyConnection.Value.GetDatabase(); var serializeddata = JsonConvert.SerializeObject(value); - await cache.StringSetAsync($"{value.Name}--{value.Surname}", serializeddata); + await cache.StringSetAsync($"{value.Id}", serializeddata); - var cacheItem = await cache.StringGetAsync($"{value.Name}--{value.Surname}"); + var cacheItem = await cache.StringGetAsync($"{value.Id}"); lazyConnection.Value.Dispose(); return cacheItem; @@ -74,11 +76,26 @@ public async Task Post([FromBody] TimesheetEntry value) // PUT api/values/5 [HttpPut("{id}")] - public async Task Put(int id, [FromBody] TimesheetEntry value) + public async Task Put(int id, [FromBody] TimesheetEntry value) { var entry = await _db.Entries.FindAsync(id); entry = value; await _db.SaveChangesAsync(); + + var lazyConnection = new Lazy(() => + { + + return ConnectionMultiplexer.Connect(_config.GetValue("CacheConnection")); + }); + + IDatabase cache = lazyConnection.Value.GetDatabase(); + + var cacheItem = await cache.StringGetAsync($"{id}"); + var serializeddata = JsonConvert.SerializeObject(value); + await cache.StringSetAsync($"{value.Id}", serializeddata); + lazyConnection.Value.Dispose(); + + return JsonConvert.SerializeObject(value); } // DELETE api/values/5 diff --git a/src/MyTimesheet/MyTimesheet/Models/TimesheetEntry.cs b/src/MyTimesheet/MyTimesheet/Models/TimesheetEntry.cs index c170246..af15280 100644 --- a/src/MyTimesheet/MyTimesheet/Models/TimesheetEntry.cs +++ b/src/MyTimesheet/MyTimesheet/Models/TimesheetEntry.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; @@ -21,8 +22,13 @@ public class TimesheetEntry */ public int Id { get; set; } + + [ForeignKey("EmployeeEntry")] public int EmployeeId { get; set; } + + [ForeignKey("ClientEntry")] public string ClientId { get; set; } + public string Project { get; set; } public DateTime Date { get; set; } public DateTime TimeStart { get; set; }