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 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()
{

View File

@ -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",
@ -77,7 +77,7 @@ namespace Serwer.Api
{
if (env.IsDevelopment())
{
app.UseCors(builder =>
app.UseCors(builder =>
{
builder.AllowAnyHeader();
builder.AllowAnyMethod();
@ -90,8 +90,8 @@ namespace Serwer.Api
app.UseRouting();
app.UseMiddleware(typeof(ExceptionHandlerMiddleware));
app.UseAuthentication();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>

View File

@ -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)

View File

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

View File

@ -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);
}
}

View File

@ -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);
}
}
}

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