public async Task <UserEnvelope> Handle(Query request, CancellationToken cancellationToken) { var currentUsername = _currentUserAccessor.GetCurrentUsername(); var person = await _context.Persons .AsNoTracking() .FirstOrDefaultAsync(x => x.Username == request.Username, cancellationToken); if (person == null) { throw new RestException(HttpStatusCode.NotFound, new { User = Constants.NOT_FOUND }); } if (_currentUserAccessor.GetCurrentUserType().Equals(UserConstants.User) && !currentUsername.Equals(person.Username) ) { throw new RestException(HttpStatusCode.Unauthorized, new { User = Constants.UNAUTHERIZE }); } var user = _mapper.Map <Person, User>(person); user.Type = UserConstants.GetUserTypeString(person.UserType); if (!user.Username.Equals(currentUsername)) { return(new UserEnvelope(user)); } // To mark the profile UI is current user user.IsCurrentUser = true; return(new UserEnvelope(user)); }
public async Task <UserEnvelope> Handle(Command request, CancellationToken cancellationToken) { if (_currentUserAccessor.GetCurrentUserType().Equals(UserConstants.User)) { throw new RestException(HttpStatusCode.Unauthorized, new { Username = Constants.UNAUTHERIZE }); } if (await _context.Persons.Where(x => x.Username == request.User.Username).AsNoTracking().AnyAsync(cancellationToken)) { throw new RestException(HttpStatusCode.BadRequest, new { Username = Constants.IN_USE }); } var person = new Person { Username = request.User.Username, ProfileUrl = request.User.ProfileUrl, Phone = request.User.Phone, UserType = request.User.UserType, Password = _passwordHasher.Hash(request.User.Password), CreatedDate = DateTime.Now }; await _context.Persons.AddAsync(person, cancellationToken); await _context.SaveChangesAsync(cancellationToken); var auditId = _currentUserAccessor.GetAuditId(); var user = _mapper.Map <Person, User>(person); user.Type = UserConstants.GetUserTypeString(person.UserType); user.Token = _jwtTokenGenerator.CreateToken(user.Username, user.Type, auditId.ToString()); return(new UserEnvelope(user)); }