public async Task <UserEnvelope> Handle(Command message, CancellationToken cancellationToken) { if (await _context.Persons.Where(x => x.Username == message.User.Username).AnyAsync(cancellationToken)) { throw new RestException(HttpStatusCode.BadRequest, new { Username = Constants.IN_USE }); } if (await _context.Persons.Where(x => x.Email == message.User.Email).AnyAsync(cancellationToken)) { throw new RestException(HttpStatusCode.BadRequest, new { Email = Constants.IN_USE }); } var salt = Guid.NewGuid().ToByteArray(); var person = new Persons { Username = message.User.Username, Email = message.User.Email, Hash = _passwordHasher.Hash(message.User.Password, salt), Salt = salt }; _context.Persons.Add(person); await _context.SaveChangesAsync(cancellationToken); var user = _mapper.Map <Domain.Persons, User>(person); user.Token = await _jwtTokenGenerator.CreateToken(person.Username); return(new UserEnvelope(user)); }
public async Task <BitRequestEnvelope> Handle(Command message, CancellationToken cancellationToken) { //var author = await _context.Persons.FirstAsync(x => x.Username == _currentUserAccessor.GetCurrentUsername(), cancellationToken); if (message.BitRequest.ReqList == null) { throw new ArgumentException($"Отсутствует список типов запрашиваемых документов"); } var checkContract = await _context.Contracts.Where(x => x.IdPkb == Int64.Parse(message.BitRequest.IdPkb)).FirstOrDefaultAsync(); if (checkContract == null) { throw new ArgumentException($"Контракт ID {message.BitRequest.IdPkb} не найден в БД"); } var checkIdBit = await _context.BitRequests.Where(x => x.IdBit == message.BitRequest.Id).FirstOrDefaultAsync(); if (checkIdBit != null) { throw new ArgumentException($"ID Bit {message.BitRequest.Id} уже существует в БД"); } var newBitRequest = new BitRequest() { IdBit = message.BitRequest.Id, IdPkb = Int64.Parse(message.BitRequest.IdPkb), Status = BitRequestStatus.New, ReqCount = message.BitRequest.ReqList.Count(), Fio = message.BitRequest.Fio }; foreach (var req in (message.BitRequest.ReqList ?? Enumerable.Empty <ReqData>())) { var newRequestDetail = new ContractRequess(newBitRequest, checkContract.Id, Int32.Parse(req.Req_tp)); newBitRequest.ContractRequests.Add(newRequestDetail); await _context.BitRequests.AddAsync(newBitRequest, cancellationToken); } try { await _context.SaveChangesAsync(cancellationToken); } catch (Exception ex) { var tt = ex.Message; } return(new BitRequestEnvelope(newBitRequest)); }
public async Task <UserEnvelope> Handle(Command message, CancellationToken cancellationToken) { var currentUsername = _currentUserAccessor.GetCurrentUsername(); var person = await _context.Persons.Where(x => x.Username == currentUsername).FirstOrDefaultAsync(cancellationToken); person.Username = message.User.Username ?? person.Username; person.Email = message.User.Email ?? person.Email; person.Bio = message.User.Bio ?? person.Bio; person.Image = message.User.Image ?? person.Image; if (!string.IsNullOrWhiteSpace(message.User.Password)) { var salt = Guid.NewGuid().ToByteArray(); person.Hash = _passwordHasher.Hash(message.User.Password, salt); person.Salt = salt; } await _context.SaveChangesAsync(cancellationToken); return(new UserEnvelope(_mapper.Map <Domain.Persons, User>(person))); }