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)); }
public async Task <CustomerEnvelope> Handle(Command request, CancellationToken cancellationToken) { #region Checking card var card = await _context.Cards .FirstOrDefaultAsync(c => c.CardNumber == request.CardNumber, cancellationToken); if (card == null) { throw new RestException(HttpStatusCode.NotFound, new { Customer = "The card is not exist" }); } if (card.CustomerId != 0) { throw new RestException(HttpStatusCode.NotFound, new { Customer = "The card is already used." }); } #endregion // Insert customer var auditId = _currentUserAccessor.GetAuditId(); var customer = new Domain.Customer { AuditId = auditId, CreatedDate = DateTime.Now, Name = request.Name, Phone = request.Phone, }; await _context.Customers.AddAsync(customer, cancellationToken); // get id customer from database await _context.SaveChangesAsync(cancellationToken); // Updating card card.CustomerId = customer.Id; _context.Update(card); await _context.SaveChangesAsync(cancellationToken); var response = _mapper.Map <Domain.Customer, Customer>(customer); response.IsCardActive = card.IsActive; return(new CustomerEnvelope(response)); }
public async Task <CardEnvelope> Handle(Command request, CancellationToken cancellationToken) { if (await _context.Cards.Where(x => x.CardNumber == request.CardNumber).AnyAsync(cancellationToken)) { throw new RestException(HttpStatusCode.BadRequest, new { Card = Constants.ALREADY_EXIST }); } var auditId = _currentUserAccessor.GetAuditId(); var cardData = new Domain.Card { CardNumber = request.CardNumber, IsActive = true, CreatedDate = DateTime.Now, AuditId = auditId }; await _context.Cards.AddAsync(cardData, cancellationToken); await _context.SaveChangesAsync(cancellationToken); var card = _mapper.Map <Domain.Card, Card>(cardData); return(new CardEnvelope(card)); }