POS_Sprint_3 #5
@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Serwer.Infrastructure.DTO;
|
||||
using Serwer.Infrastructure.Services;
|
||||
@ -35,6 +36,14 @@ namespace Serwer.Api.Controllers
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpPost("Update")]
|
||||
public async Task<IActionResult> Update([FromForm]UpdateUserModel request)
|
||||
{
|
||||
var user = await _userService.UpdateAsync(request);
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
[HttpGet("Test")]
|
||||
public IActionResult Test()
|
||||
{
|
||||
|
@ -44,7 +44,7 @@ namespace Serwer.Api
|
||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Serwer.Api", Version = "v1" });
|
||||
});
|
||||
|
||||
var hostEnviroment = new HostEnviroment { RootPath = WebRootPath};
|
||||
var hostEnviroment = new HostEnviroment { RootPath = WebRootPath };
|
||||
var jwtSettings = new JwtSettings()
|
||||
{
|
||||
Issuer = "PoszukiwaczInc",
|
||||
|
@ -18,14 +18,15 @@ namespace Serwer.Core.Domain
|
||||
public DateTime UpdatedAt { get; protected set; }
|
||||
|
||||
protected User() { }
|
||||
public User(string email, string name, string surname, string login, string password)
|
||||
public User(Guid id, string email, string name, string surname, string login, string password)
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
Id = id;
|
||||
SetEmail(email);
|
||||
SetName(name);
|
||||
SetSurname(surname);
|
||||
SetLogin(login);
|
||||
SetPassword(password);
|
||||
CreatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
public void SetEmail(string email)
|
||||
|
@ -8,6 +8,7 @@ namespace Serwer.Infrastructure.DTO
|
||||
{
|
||||
public class UserDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Surname { get; set; }
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Serwer.Infrastructure.DTO;
|
||||
using Serwer.Infrastructure.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -11,5 +12,6 @@ namespace Serwer.Infrastructure.Services
|
||||
{
|
||||
Task RegisterAsync(string email, string name, string surname, string login, string password);
|
||||
Task<SignedUserDto> SignInAsync(string login, string password);
|
||||
Task<UserDto> UpdateAsync(UpdateUserModel userModel);
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ using Serwer.Core.Domain;
|
||||
using Serwer.Core.Repositories;
|
||||
using Serwer.Infrastructure.DTO;
|
||||
using Serwer.Infrastructure.Mappers;
|
||||
using Serwer.Infrastructure.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -30,7 +31,7 @@ namespace Serwer.Infrastructure.Services
|
||||
{
|
||||
throw new Exception($"User with login: {login} already exists.");
|
||||
}
|
||||
var user = new User(email, name, surname, login, password);
|
||||
var user = new User(Guid.NewGuid(), email, name, surname, login, password);
|
||||
await _userRepository.AddAsync(user);
|
||||
}
|
||||
|
||||
@ -54,5 +55,24 @@ namespace Serwer.Infrastructure.Services
|
||||
Jwt = _mapper.Map<JwtDto>(jwt)
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<UserDto> UpdateAsync(UpdateUserModel userModel)
|
||||
{
|
||||
var user = await _userRepository.GetAsync(userModel.Id);
|
||||
if(user == null)
|
||||
{
|
||||
throw new Exception("User not found.");
|
||||
}
|
||||
var userLoginTaken = await _userRepository.GetAsync(userModel.Login);
|
||||
if(userLoginTaken != null && userLoginTaken.Id != user.Id)
|
||||
{
|
||||
throw new Exception("User login taken.");
|
||||
}
|
||||
|
||||
var newUser = new User(user.Id, userModel.Email, userModel.Name, userModel.Surname, userModel.Login, userModel.Password);
|
||||
await _userRepository.UpdateAsync(newUser);
|
||||
newUser = await _userRepository.GetAsync(user.Id);
|
||||
return _mapper.Map<UserDto>(newUser);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
18
Serwer/Serwer.Infrastructure/ViewModels/UpdateUserModel.cs
Normal file
18
Serwer/Serwer.Infrastructure/ViewModels/UpdateUserModel.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.ViewModels
|
||||
{
|
||||
public class UpdateUserModel
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Surname { get; set; }
|
||||
public string Login { get; set; }
|
||||
public string Password { get; set; } = null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user