From f64af6e0afd7b2033be5defb97c5d55277edb00d Mon Sep 17 00:00:00 2001 From: Marcus Ferl Date: Sat, 8 Jul 2023 21:10:18 +0200 Subject: [PATCH] Added user api calls and UserRepo --- Weiferl.Api/Controllers/Users.cs | 92 ++++++++++++++++++++++++++++++ Weiferl.Api/Repos/UserRepo.cs | 97 ++++++++++++++++++++++++++++++++ Weiferl.Api/Weiferl.Api.csproj | 2 +- 3 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 Weiferl.Api/Controllers/Users.cs create mode 100644 Weiferl.Api/Repos/UserRepo.cs diff --git a/Weiferl.Api/Controllers/Users.cs b/Weiferl.Api/Controllers/Users.cs new file mode 100644 index 0000000..f012699 --- /dev/null +++ b/Weiferl.Api/Controllers/Users.cs @@ -0,0 +1,92 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Weiferl.Api.Repos; +using Weiferl.Databas.EF.Models; + +namespace Weiferl.Api.Controllers +{ + [ApiController] + [Route("api")] + public class Users : ControllerBase + { + private readonly UserRepo userRepo; + + public Users() + { + userRepo = new UserRepo(); + } + [HttpGet("getAllUsers")] + public async Task GetAllUsers() + { + try + { + var users = await userRepo.GetAllUsers(); + return Ok(users); + } + catch (Exception ex) + { + return BadRequest(ex); + } + } + + [HttpPost("addUser")] + public async Task AddUser() + { + var user = new User + { + UserId = Guid.NewGuid(), + Firstname = "Marcus", + Lastname = "Ferl", + Birthday = new DateTime(1986, 01, 17), + Email = "marcus@weifer.de", + IsDeleted = false, + }; + + try + { + await userRepo.CreateUser(user); + return Ok(); + } + catch (Exception ex) + { + return BadRequest(ex); + } + } + [HttpPost("updateUser")] + public async Task UpdateUser() + { + var user = new User + { + UserId = Guid.NewGuid(), + Firstname = "Susanne", + Lastname = "Weik", + Birthday = new DateTime(1985, 02, 21), + Email = "susanne@weifer.de", + IsDeleted = false, + }; + try + { + await userRepo.UpdateUser(user, 1); + return Ok(); + } + catch (Exception ex) + { + return BadRequest(ex); + } + } + + [HttpPost("removeUser")] + public async Task RemoveUser() + { + try + { + await userRepo.RemoveUser(1); + return Ok(); + } + catch (Exception ex) + { + return BadRequest(ex); + } + } + } +} diff --git a/Weiferl.Api/Repos/UserRepo.cs b/Weiferl.Api/Repos/UserRepo.cs new file mode 100644 index 0000000..aa38143 --- /dev/null +++ b/Weiferl.Api/Repos/UserRepo.cs @@ -0,0 +1,97 @@ +using Microsoft.EntityFrameworkCore; +using Weiferl.Databas.EF; +using Weiferl.Databas.EF.Models; + +namespace Weiferl.Api.Repos +{ + public class UserRepo + { + readonly private HardwareShopContext dbContext; + + public UserRepo() + { + this .dbContext = new HardwareShopContext(); + } + + public async Task CreateUser(User user) + { + try + { + dbContext.User.Add(user); + dbContext.SaveChanges(); + } + catch (Exception ex) + { + throw ex; + } + } + public async Task GetUser(int id) + { + try + { + var user = dbContext.User.FirstOrDefault(u => u.Id == id); + return user; + } + catch (Exception ex) + { + throw ex; + } + } + + public async Task> GetAllUsers() + { + try + { + var users = dbContext.User.ToList(); + return users; + } + catch (Exception ex) + { + throw ex; + } + } + public async Task UpdateUser(User updatedUser, int id) + { + var user = await dbContext.User.FindAsync(id); + + if(user != null) + { + user.Firstname = updatedUser.Firstname; + user.Lastname = updatedUser.Lastname; + user.Birthday = updatedUser.Birthday; + user.Email = updatedUser.Email; + user.IsDeleted = updatedUser.IsDeleted; + } + try + { + await dbContext.SaveChangesAsync(); + } + catch (Exception ex) + { + throw ex; + } + } + public async Task RemoveUser(int id) + { + var user = await dbContext.User.FindAsync(id); + + if(user != null) + { + user.Firstname = "Deleted"; + user.Lastname = "User"; + user.Birthday = DateTime.Now; + user.Email = "@@@"; + user.IsDeleted = true; + } + try + { + await dbContext.SaveChangesAsync(); + } + catch (Exception ex) + { + throw ex; + } + + } + } +} diff --git a/Weiferl.Api/Weiferl.Api.csproj b/Weiferl.Api/Weiferl.Api.csproj index 1e38b2f..24d7612 100644 --- a/Weiferl.Api/Weiferl.Api.csproj +++ b/Weiferl.Api/Weiferl.Api.csproj @@ -12,7 +12,7 @@ - +