Added user api calls and UserRepo
This commit is contained in:
parent
4c201db61b
commit
f64af6e0af
92
Weiferl.Api/Controllers/Users.cs
Normal file
92
Weiferl.Api/Controllers/Users.cs
Normal file
|
@ -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<IActionResult> GetAllUsers()
|
||||
{
|
||||
try
|
||||
{
|
||||
var users = await userRepo.GetAllUsers();
|
||||
return Ok(users);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(ex);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("addUser")]
|
||||
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> RemoveUser()
|
||||
{
|
||||
try
|
||||
{
|
||||
await userRepo.RemoveUser(1);
|
||||
return Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
97
Weiferl.Api/Repos/UserRepo.cs
Normal file
97
Weiferl.Api/Repos/UserRepo.cs
Normal file
|
@ -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<User> GetUser(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = dbContext.User.FirstOrDefault(u => u.Id == id);
|
||||
return user;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<User>> 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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -12,7 +12,7 @@
|
|||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.8" />
|
||||
<PackageReference Include="MySql.EntityFrameworkCore" Version="7.0.2" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
<PackageReference Include="Weiferl.Databas.EF" Version="1.0.1" />
|
||||
<PackageReference Include="Weiferl.Databas.EF" Version="1.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
Loading…
Reference in New Issue
Block a user