This commit is contained in:
s426226 2020-12-18 16:04:07 +01:00
parent 11f96f9d06
commit 04ad1f02f4
7 changed files with 59 additions and 8 deletions

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Serwer.Infrastructure.DTO; using Serwer.Infrastructure.DTO;
using Serwer.Infrastructure.Services; using Serwer.Infrastructure.Services;
@ -35,6 +36,14 @@ namespace Serwer.Api.Controllers
return Ok(user); 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")] [HttpGet("Test")]
public IActionResult Test() public IActionResult Test()
{ {

View File

@ -18,14 +18,15 @@ namespace Serwer.Core.Domain
public DateTime UpdatedAt { get; protected set; } public DateTime UpdatedAt { get; protected set; }
protected User() { } 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); SetEmail(email);
SetName(name); SetName(name);
SetSurname(surname); SetSurname(surname);
SetLogin(login); SetLogin(login);
SetPassword(password); SetPassword(password);
CreatedAt = DateTime.UtcNow;
} }
public void SetEmail(string email) public void SetEmail(string email)

View File

@ -8,6 +8,7 @@ namespace Serwer.Infrastructure.DTO
{ {
public class UserDto public class UserDto
{ {
public Guid Id { get; set; }
public string Email { get; set; } public string Email { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Surname { get; set; } public string Surname { get; set; }

View File

@ -1,4 +1,5 @@
using Serwer.Infrastructure.DTO; using Serwer.Infrastructure.DTO;
using Serwer.Infrastructure.ViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -11,5 +12,6 @@ namespace Serwer.Infrastructure.Services
{ {
Task RegisterAsync(string email, string name, string surname, string login, string password); Task RegisterAsync(string email, string name, string surname, string login, string password);
Task<SignedUserDto> SignInAsync(string login, string password); Task<SignedUserDto> SignInAsync(string login, string password);
Task<UserDto> UpdateAsync(UpdateUserModel userModel);
} }
} }

View File

@ -3,6 +3,7 @@ using Serwer.Core.Domain;
using Serwer.Core.Repositories; using Serwer.Core.Repositories;
using Serwer.Infrastructure.DTO; using Serwer.Infrastructure.DTO;
using Serwer.Infrastructure.Mappers; using Serwer.Infrastructure.Mappers;
using Serwer.Infrastructure.ViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -30,7 +31,7 @@ namespace Serwer.Infrastructure.Services
{ {
throw new Exception($"User with login: {login} already exists."); 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); await _userRepository.AddAsync(user);
} }
@ -54,5 +55,24 @@ namespace Serwer.Infrastructure.Services
Jwt = _mapper.Map<JwtDto>(jwt) 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);
}
} }
} }

View 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;
}
}