Skip to content

Commit

Permalink
Merge pull request #455 from SharebookBR/develop
Browse files Browse the repository at this point in the history
Develop to master
  • Loading branch information
raffacabofrio authored Dec 8, 2021
2 parents fea35d8 + a07a0cd commit 92d2be5
Show file tree
Hide file tree
Showing 23 changed files with 734 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using AutoMapper;
using ShareBook.Api.ViewModels;
using ShareBook.Domain;

using ShareBook.Domain.DTOs;

namespace ShareBook.Api.AutoMapper
{
public class ViewModelToDomainMappingProfile : Profile
Expand All @@ -24,7 +25,7 @@ protected ViewModelToDomainMappingProfile(string profileName) : base(profileName
#region [ User ]

CreateMap<LoginUserVM, User>();
CreateMap<RegisterUserVM, User>()
CreateMap<RegisterUserDTO, User>()
.ForPath(dest => dest.Address.Street, opt => opt.MapFrom(src => src.Street))
.ForPath(dest => dest.Address.Number, opt => opt.MapFrom(src => src.Number))
.ForPath(dest => dest.Address.PostalCode, opt => opt.MapFrom(src => src.PostalCode))
Expand Down
31 changes: 24 additions & 7 deletions ShareBook/ShareBook.Api/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using ShareBook.Api.ViewModels;
using ShareBook.Domain;
using ShareBook.Domain.Common;
using ShareBook.Domain.DTOs;
using ShareBook.Domain.Exceptions;
using ShareBook.Infra.CrossCutting.Identity;
using ShareBook.Infra.CrossCutting.Identity.Interfaces;
Expand Down Expand Up @@ -102,14 +103,18 @@ public async Task<IActionResult> WhoAccessedMyProfile(Guid userId)
[HttpPost("Register")]
[ProducesResponseType(typeof(object), 200)]
[ProducesResponseType(409)]
public IActionResult Post([FromBody] RegisterUserVM registerUserVM, [FromServices] SigningConfigurations signingConfigurations, [FromServices] TokenConfigurations tokenConfigurations)
public IActionResult Post([FromBody] RegisterUserDTO registerUserDto, [FromServices] SigningConfigurations signingConfigurations, [FromServices] TokenConfigurations tokenConfigurations)
{
var user = _mapper.Map<User>(registerUserVM);

var result = _userService.Insert(user);

if (result.Success)
return Ok(_signManager.GenerateTokenAndSetIdentity(result.Value, signingConfigurations, tokenConfigurations));
var result = _userService.Insert(registerUserDto);

if (result.Success)
{
if (registerUserDto.Age > 12)
return Ok(_signManager.GenerateTokenAndSetIdentity(result.Value, signingConfigurations, tokenConfigurations));
else
return Ok(new Result(SuccessMessage: "Seu cadastro foi realizado com sucesso. Foi enviado um email para os pais solicitando o consentimento. Vamos te avisar por email quando seu acesso for liberado. Obrigado. =)"));
}


return Conflict(result);
}
Expand Down Expand Up @@ -215,6 +220,18 @@ public IActionResult ChangeUserPasswordByHashCode([FromBody] ChangeUserPasswordB
return Ok(resultChangePasswordUser);
}

[HttpPut("ParentAproval")]
public IActionResult ParentAproval([FromBody] ParentAprovalVM parentAprovalVM)
{
var ParentHashCodeAproval = parentAprovalVM.ParentHashCodeAproval;

if (string.IsNullOrEmpty(ParentHashCodeAproval) || !Guid.TryParse(ParentHashCodeAproval, out _))
throw new ShareBookException("Código inválido.");

_userService.ParentAproval(ParentHashCodeAproval);
return Ok();
}

#endregion PUT

private bool IsValidClientVersion(string client, string clientVersion)
Expand Down
38 changes: 29 additions & 9 deletions ShareBook/ShareBook.Api/Controllers/BookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
using System.Threading.Tasks;
using Flurl.Util;
using ShareBook.Domain.Enums;

using ShareBook.Domain.Exceptions;

namespace ShareBook.Api.Controllers
{
[Route("api/[controller]")]
Expand Down Expand Up @@ -220,6 +221,10 @@ public PagedList<BookVM> ByCategoryId(Guid categoryId, int page, int items)
[ProducesResponseType(typeof(Result), 200)]
public IActionResult RequestBook([FromBody] RequestBookVM requestBookVM)
{
User user = GetUser();
if (_IsDonator(requestBookVM.BookId, user) && !_IsAdmin(user)) //Permitido solicitar o próprio livro somente para Admin
throw new ShareBookException("Não é possivel solicitar esse livro pois você é o doador.");

_bookUserService.Insert(requestBookVM.BookId, requestBookVM.Reason);
return Ok(new Result { SuccessMessage = "Pedido realizado com sucesso!" });
}
Expand Down Expand Up @@ -389,21 +394,36 @@ public IActionResult RenewChooseDate(Guid bookId)
// apenas doador e adm
private bool _IsBookOwner(Guid bookId)
{
var userId = new Guid(Thread.CurrentPrincipal?.Identity?.Name);
var user = _userService.Find(userId);
User user = GetUser();
if (user == null)
return false;

// Adm
if (user.Profile == Domain.Enums.Profile.Administrator)
return true;
if (_IsAdmin(user)) return true;

// Doador
var book = _service.GetBookWithAllUsers(bookId);
if (book.UserId == userId)
return true;
return _IsDonator(bookId, user);
}

return false;
private bool _IsDonator(Guid bookId, User user)
{
if (user == null || user.Id == Guid.Empty) return false;
Book book = _service.GetBookWithAllUsers(bookId);
if (book == null || book.Id == Guid.Empty) return false;

return book.UserId == user.Id;
}

private User GetUser()
{
var userId = new Guid(Thread.CurrentPrincipal?.Identity?.Name);
return _userService.Find(userId);
}

private bool _IsAdmin(User user)
{
if (user == null || user?.Profile == null) return false;
return user.Profile.Equals(Domain.Enums.Profile.Administrator);
}

// doador, adm e ganhador
Expand Down
2 changes: 1 addition & 1 deletion ShareBook/ShareBook.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ShareBook.Api
public class Program
{
public static void Main(string[] args)
{
{
BuildWebHost(args).Run();
}

Expand Down
7 changes: 7 additions & 0 deletions ShareBook/ShareBook.Api/ViewModels/ParentAprovalVM.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ShareBook.Api.ViewModels
{
public class ParentAprovalVM
{
public string ParentHashCodeAproval { get; set; }
}
}
61 changes: 0 additions & 61 deletions ShareBook/ShareBook.Api/ViewModels/RegisterUserVM.cs

This file was deleted.

3 changes: 0 additions & 3 deletions ShareBook/ShareBook.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,5 @@
"QueueUrl": "",
"Region": "",
"MaxDestinationsPerMessage": 5
},
"ClientSettings": {
"AndroidMinVersion": "v1.0.0"
}
}
3 changes: 0 additions & 3 deletions ShareBook/ShareBook.Api/appsettings.Stage.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,5 @@
"QueueUrl": "",
"Region": "",
"MaxDestinationsPerMessage": 5
},
"ClientSettings": {
"AndroidMinVersion": "v1.0.0"
}
}
3 changes: 3 additions & 0 deletions ShareBook/ShareBook.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
"Default": "Warning"
}
}
},
"ClientSettings": {
"AndroidMinVersion": "v1.3.0"
}
}
38 changes: 38 additions & 0 deletions ShareBook/ShareBook.Domain/DTOs/RegisterUserDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

namespace ShareBook.Domain.DTOs
{
public class RegisterUserDTO
{
public string Name { get; set; }

public string Email { get; set; }

public string Street { get; set; }

public string Number { get; set; }

public string Complement { get; set; }

public string Neighborhood { get; set; }

public string PostalCode { get; set; }

public string City { get; set; }

public string State { get; set; }

public string Country { get; set; }

public string Linkedin { get; set; }

public string Phone { get; set; }

public string Password { get; set; }

public bool AllowSendingEmail { get; set; } = true;

public int Age { get; set; }

public string ParentEmail { get; set; }
}
}
5 changes: 5 additions & 0 deletions ShareBook/ShareBook.Domain/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public class User : BaseEntity
public virtual ICollection<Book> BooksDonated { get; set; }
public virtual ICollection<AccessHistory> Visitors { get; set; }

public string ParentEmail { get; set; }
public string ParentHashCodeAproval { get; set; }
public bool ParentAproved { get; set; } = true;


public bool PasswordIsStrong()
{
Regex rgx = new Regex(@"(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^a-zA-Z0-9])[A-Za-z0-9\d$@$!%_*_?&#.,-_:;]{8,}");
Expand Down
Loading

0 comments on commit 92d2be5

Please sign in to comment.