From 454606c698444f9e8c82498c8141ded573bceb60 Mon Sep 17 00:00:00 2001 From: JosephJohncross Date: Fri, 7 Apr 2023 22:40:26 +0100 Subject: [PATCH] feat:Implementation of Generic repo and clean architecture --- .../Application/Application.csproj | 23 ++++ .../Application/Class1.cs | 5 + .../Common/DTOs/Users/UserForCreate.cs | 12 ++ .../Common/DTOs/Users/UserForDisplayDto.cs | 16 +++ .../Application/Data/ApplicationDbContext.cs | 10 ++ .../Application/GlobalUsing.cs | 3 + ...230403040521_InitialMigrations.Designer.cs | 61 +++++++++ .../20230403040521_InitialMigrations.cs | 39 ++++++ .../ApplicationDbContextModelSnapshot.cs | 58 ++++++++ .../Application/Users/IRepository.cs | 18 +++ .../Application/Users/Repository.cs | 71 ++++++++++ .../GenericRepoAndOnionArchi/Domain/Class1.cs | 5 + .../Domain/Common/BaseEntity.cs | 15 ++ .../Domain/Domain.csproj | 9 ++ .../Domain/Entities/User.cs | 17 +++ .../Domain/GlobalUsing.cs | 0 .../Infrastructure/Class1.cs | 5 + .../Infrastructure/GlobalUsing.cs | 3 + .../Infrastructure/Infrastructure.csproj | 14 ++ .../Infrastructure/Services/IUserService.cs | 10 ++ .../Infrastructure/Services/UserService.cs | 121 +++++++++++++++++ .../UI/Controllers/UserController.cs | 128 ++++++++++++++++++ .../UI/GlobalUsing.cs | 5 + .../GenericRepoAndOnionArchi/UI/Program.cs | 28 ++++ .../UI/Properties/launchSettings.json | 41 ++++++ .../GenericRepoAndOnionArchi/UI/UI.csproj | 24 ++++ .../UI/appsettings.Development.json | 11 ++ .../UI/appsettings.json | 12 ++ 28 files changed, 764 insertions(+) create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Application.csproj create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Class1.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Common/DTOs/Users/UserForCreate.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Common/DTOs/Users/UserForDisplayDto.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Data/ApplicationDbContext.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/GlobalUsing.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Persistence/Migrations/20230403040521_InitialMigrations.Designer.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Persistence/Migrations/20230403040521_InitialMigrations.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Persistence/Migrations/ApplicationDbContextModelSnapshot.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Users/IRepository.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Users/Repository.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Class1.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Common/BaseEntity.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Domain.csproj create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Entities/User.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/GlobalUsing.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Class1.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/GlobalUsing.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Infrastructure.csproj create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Services/IUserService.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Services/UserService.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/Controllers/UserController.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/GlobalUsing.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/Program.cs create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/Properties/launchSettings.json create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/UI.csproj create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/appsettings.Development.json create mode 100644 Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/appsettings.json diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Application.csproj b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Application.csproj new file mode 100644 index 0000000..a0fdb2c --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Application.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + enable + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Class1.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Class1.cs new file mode 100644 index 0000000..b1581ab --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Class1.cs @@ -0,0 +1,5 @@ +namespace Application; +public class Class1 +{ + +} diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Common/DTOs/Users/UserForCreate.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Common/DTOs/Users/UserForCreate.cs new file mode 100644 index 0000000..3f5062c --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Common/DTOs/Users/UserForCreate.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Application.Common.DTOs.Users +{ + public class UserForCreate + { + + } +} \ No newline at end of file diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Common/DTOs/Users/UserForDisplayDto.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Common/DTOs/Users/UserForDisplayDto.cs new file mode 100644 index 0000000..ed0416d --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Common/DTOs/Users/UserForDisplayDto.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Application.Common.DTOs.Users +{ + public class UserForDisplayDto + { + public string FirstName { get; set; } + public string LastName { get; set; } + public int Age { get; set; } + public string MaritalStatus { get; set; } + public string Address { get; set; } + } +} \ No newline at end of file diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Data/ApplicationDbContext.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Data/ApplicationDbContext.cs new file mode 100644 index 0000000..cc6f355 --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Data/ApplicationDbContext.cs @@ -0,0 +1,10 @@ + +namespace Application.Data +{ + public class ApplicationDbContext : DbContext + { + public ApplicationDbContext(DbContextOptions options) : base(options) { } + + public DbSet Users { get; set; } + } +} \ No newline at end of file diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/GlobalUsing.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/GlobalUsing.cs new file mode 100644 index 0000000..316c32d --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/GlobalUsing.cs @@ -0,0 +1,3 @@ +global using Microsoft.EntityFrameworkCore; +global using Domain.Entities; +global using Domain.Common; \ No newline at end of file diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Persistence/Migrations/20230403040521_InitialMigrations.Designer.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Persistence/Migrations/20230403040521_InitialMigrations.Designer.cs new file mode 100644 index 0000000..1aacd70 --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Persistence/Migrations/20230403040521_InitialMigrations.Designer.cs @@ -0,0 +1,61 @@ +// +using Application.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Application.Persistence.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20230403040521_InitialMigrations")] + partial class InitialMigrations + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Domain.Entities.User", b => + { + b.Property("UserId") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("UserId")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("Age") + .HasColumnType("integer"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("text"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("text"); + + b.Property("MaritalStatus") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("UserId"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Persistence/Migrations/20230403040521_InitialMigrations.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Persistence/Migrations/20230403040521_InitialMigrations.cs new file mode 100644 index 0000000..a18ebdf --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Persistence/Migrations/20230403040521_InitialMigrations.cs @@ -0,0 +1,39 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Application.Persistence.Migrations +{ + /// + public partial class InitialMigrations : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + UserId = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + FirstName = table.Column(type: "text", nullable: false), + LastName = table.Column(type: "text", nullable: false), + Age = table.Column(type: "integer", nullable: false), + MaritalStatus = table.Column(type: "text", nullable: false), + Address = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.UserId); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Persistence/Migrations/ApplicationDbContextModelSnapshot.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Persistence/Migrations/ApplicationDbContextModelSnapshot.cs new file mode 100644 index 0000000..bbab640 --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Persistence/Migrations/ApplicationDbContextModelSnapshot.cs @@ -0,0 +1,58 @@ +// +using Application.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Application.Persistence.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + partial class ApplicationDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Domain.Entities.User", b => + { + b.Property("UserId") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("UserId")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("Age") + .HasColumnType("integer"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("text"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("text"); + + b.Property("MaritalStatus") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("UserId"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Users/IRepository.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Users/IRepository.cs new file mode 100644 index 0000000..6f3fedd --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Users/IRepository.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Application.Users +{ + public interface IRepository where T : BaseEntity + { + IEnumerable GetAll(); + T Get(int Id); + List Filter(string name); + void Insert(T entity); + void Update(T entity); + void Delete(T entity); + void SaveChanges(); + } +} \ No newline at end of file diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Users/Repository.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Users/Repository.cs new file mode 100644 index 0000000..7cd66e9 --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Application/Users/Repository.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Application.Data; + +namespace Application.Users +{ + public class Repository : IRepository where T : User + { + private readonly ApplicationDbContext _context; + private DbSet entities; + public Repository(ApplicationDbContext context) + { + _context = context; + entities = _context.Set(); + } + + public IEnumerable GetAll() + { + return entities.AsEnumerable(); + } + + public T Get(int Id) + { + return entities.SingleOrDefault(u => u.UserId == Id); + } + + public List Filter(string name) + { + var obj = entities.Where(e => e.FirstName.ToLower() == name.ToLower()).Select(x=>x).ToList(); + Console.WriteLine(obj); + return obj; + } + + public void Insert(T entity) + { + if (entity == null) + { + throw new ArgumentNullException("entity"); + } + entities.Add(entity); + _context.SaveChanges(); + } + + public void Update(T entity) + { + if (entity == null) + { + throw new ArgumentNullException("entity"); + } + entities.Update(entity); + _context.SaveChanges(); + } + + public void Delete(T entity) + { + if (entity == null) + { + throw new ArgumentNullException("entity"); + } + entities.Remove(entity); + _context.SaveChanges(); + } + + public void SaveChanges() + { + _context.SaveChanges(); + } + } +} \ No newline at end of file diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Class1.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Class1.cs new file mode 100644 index 0000000..f0290e9 --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Class1.cs @@ -0,0 +1,5 @@ +namespace Domain; +public class Class1 +{ + +} diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Common/BaseEntity.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Common/BaseEntity.cs new file mode 100644 index 0000000..bf21e31 --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Common/BaseEntity.cs @@ -0,0 +1,15 @@ +using System.Net.Mime; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations; + +namespace Domain.Common +{ + public class BaseEntity + { + [Key] + public int UserId { get; set; } + } +} \ No newline at end of file diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Domain.csproj b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Domain.csproj new file mode 100644 index 0000000..4658cbf --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Domain.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Entities/User.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Entities/User.cs new file mode 100644 index 0000000..1f8510a --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/Entities/User.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Domain.Common; + +namespace Domain.Entities +{ + public class User : BaseEntity + { + public string FirstName { get; set; } + public string LastName { get; set; } + public int Age { get; set; } + public string MaritalStatus { get; set; } + public string Address { get; set; } + } +} \ No newline at end of file diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/GlobalUsing.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Domain/GlobalUsing.cs new file mode 100644 index 0000000..e69de29 diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Class1.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Class1.cs new file mode 100644 index 0000000..b8ed309 --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Class1.cs @@ -0,0 +1,5 @@ +namespace Infrastructure; +public class Class1 +{ + +} diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/GlobalUsing.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/GlobalUsing.cs new file mode 100644 index 0000000..582cb50 --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/GlobalUsing.cs @@ -0,0 +1,3 @@ +global using Application.Users; +global using Domain.Entities; +global using Application.Common.DTOs.Users; \ No newline at end of file diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Infrastructure.csproj b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Infrastructure.csproj new file mode 100644 index 0000000..1076993 --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Infrastructure.csproj @@ -0,0 +1,14 @@ + + + + + + + + + net7.0 + enable + enable + + + diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Services/IUserService.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Services/IUserService.cs new file mode 100644 index 0000000..f3d16ae --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Services/IUserService.cs @@ -0,0 +1,10 @@ + +public interface IUserService +{ + IEnumerable GetAll(); + UserForDisplayDto Get(int Id); + List Filter(string name); + void Insert(UserForDisplayDto userDto); + void Update(int Id, UserForDisplayDto userDto); + void Delete(int Id); +} \ No newline at end of file diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Services/UserService.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Services/UserService.cs new file mode 100644 index 0000000..8e0a19d --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/Infrastructure/Services/UserService.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Infrastructure.Services +{ + public class UserService : IUserService + { + private readonly IRepository _userRepo; + public UserService(IRepository userRepo) + { + _userRepo = userRepo; + } + + public IEnumerable GetAll() + { + var allUsers = _userRepo.GetAll(); + return allUsers.Select(user => new UserForDisplayDto + { + FirstName = user.FirstName, + LastName = user.LastName, + Address = user.Address, + Age = user.Age, + MaritalStatus = user.MaritalStatus + }); + } + + public UserForDisplayDto Get(int Id) + { + try{ + var user = _userRepo.Get(Id); + return new UserForDisplayDto + { + FirstName = user.FirstName, + LastName = user.LastName, + Address = user.Address, + Age = user.Age, + MaritalStatus = user.MaritalStatus + }; + } + catch(Exception){ + throw; + } + } + + public List Filter(string name) + { + List usersDto = new List(); + var users = _userRepo.Filter(name); + foreach (var user in users){ + usersDto.Append(new UserForDisplayDto{ + Address = user.Address, + Age = user.Age, + FirstName = user.FirstName, + LastName = user.LastName, + MaritalStatus = user.MaritalStatus, + }); + } + return usersDto; + } + + public void Insert(UserForDisplayDto userDto) + { + try + { + _userRepo.Insert(new User + { + Address = userDto.Address, + Age = userDto.Age, + FirstName = userDto.FirstName, + LastName = userDto.LastName, + MaritalStatus = userDto.MaritalStatus + }); + + } + catch (Exception) + { + throw; + } + } + + public void Update(int Id, UserForDisplayDto userDto) + { + try + { + var user = _userRepo.Get(Id); + if (user != null) + { + _userRepo.Update(new User + { + Address = userDto.Address, + Age = userDto.Age, + FirstName = userDto.FirstName, + LastName = userDto.LastName, + MaritalStatus = userDto.MaritalStatus + }); + } + + } + catch (Exception) + { + throw; + } + } + + public void Delete(int Id) + { + try + { + var user = _userRepo.Get(Id); + _userRepo.Delete(user); + } + catch (Exception) + { + throw; + } + } + + } +} \ No newline at end of file diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/Controllers/UserController.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/Controllers/UserController.cs new file mode 100644 index 0000000..877e764 --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/Controllers/UserController.cs @@ -0,0 +1,128 @@ +using System.Net; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +namespace UI.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class UserController : ControllerBase + { + private readonly IUserService _userService; + public UserController(IUserService userService) + { + _userService = userService; + } + + /// + /// Create a user + /// + /// + /// Encapsulated object representing the user + [HttpPost("create_user")] + public ActionResult CreateUser(UserForDisplayDto userDto) + { + if (userDto != null) + { + _userService.Insert(userDto); + return Ok("User created successfully"); + } + return BadRequest("Something Went Wrong"); + } + + /// + /// Returns all users in the database + /// + /// An array of users + [HttpGet("get_all_users")] + public ActionResult GetAllUsers() + { + var obj = _userService.GetAll(); + if (obj == null) + { + return NotFound("No user found"); + } + return Ok(obj); + } + + /// + /// Endpoint that gets a user based on supplied Id + /// + /// user Id + /// User object + [HttpGet("get_user")] + public ActionResult GetUser(int Id) + { + try + { + var obj = _userService.Get(Id); + return Ok(obj); + } + catch(Exception) + { + return NotFound("User does not exist"); + } + } + + /// + /// Endpoint returns users matching filter query + /// + /// User first or last name + /// An array of users matching the filter query + [HttpGet("filter_user")] + public ActionResult FilterUser(string name) + { + if (name == null){ + return BadRequest(); + } + var obj = _userService.Filter(name); + Console.WriteLine(obj); + return Ok(obj); + } + + /// + /// Endpoint to update a user details + /// + /// user Id + /// User updateDetails + /// void + [HttpPatch("edit_user")] + public ActionResult EditUser(int Id, UserForDisplayDto userDto) + { + + if (userDto != null) + { + _userService.Update(Id, userDto); + return Ok("user Updated Successfully"); + } + else + { + return BadRequest(); + } + } + + /// + /// Enpoint deletes user + /// + /// User Id + /// void + [HttpPost("delete_user")] + public ActionResult DeleteUser(int Id) + { + + if (Id != null) + { + _userService.Delete(Id); + return Ok("User deleted"); + } + else + { + return BadRequest(); + } + } + + } +} \ No newline at end of file diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/GlobalUsing.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/GlobalUsing.cs new file mode 100644 index 0000000..f994e07 --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/GlobalUsing.cs @@ -0,0 +1,5 @@ +global using Application.Data; +global using Application.Common.DTOs.Users; +global using Application.Users; +global using Infrastructure.Services; +global using Microsoft.EntityFrameworkCore; \ No newline at end of file diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/Program.cs b/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/Program.cs new file mode 100644 index 0000000..4c6deed --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/Program.cs @@ -0,0 +1,28 @@ +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Services.AddDbContext(options => options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); +builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); +builder.Services.AddScoped(); + + +var app = builder.Build(); +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/Properties/launchSettings.json b/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/Properties/launchSettings.json new file mode 100644 index 0000000..7c7a062 --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:61274", + "sslPort": 44364 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5163", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7013;http://localhost:5163", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/UI.csproj b/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/UI.csproj new file mode 100644 index 0000000..bd75df5 --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/UI.csproj @@ -0,0 +1,24 @@ + + + + net7.0 + enable + enable + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/appsettings.Development.json b/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/appsettings.Development.json new file mode 100644 index 0000000..0d05b59 --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/appsettings.Development.json @@ -0,0 +1,11 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "ConnectionStrings": { + "DefaultConnection": "Host=localhost;Port=5432;Username=postgres;Password=root;Database=CleanArchitecture_Users" + } +} diff --git a/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/appsettings.json b/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/appsettings.json new file mode 100644 index 0000000..27a529e --- /dev/null +++ b/Joseph/StageFourTask/GenericRepoAndOnionArchi/UI/appsettings.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "ConnectionStrings": { + "DefaultConnection": "" + } +}