/// <summary> /// Saves all changes made in this context to the underlying database. /// </summary> /// <returns> /// A <see cref="Task" /> number of objects written to the underlying database. /// </returns> public async Task SaveAsync() { try { BeginTransaction(); //saves all operations within one transaction await _dataContext.SaveChangesAsync(); Commit(); } catch (DbEntityValidationException ex) { foreach (var validationErrors in ex.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { log.ErrorFormat("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } Rollback(); throw; } catch (Exception ex) { log.Error(ex); Rollback(); throw; } }
public async Task <UserDto> Handle(UpdateUserInfo request, CancellationToken cancellationToken) { var validator = new UpdateUserInfoValidator(); ValidationResult validationResult = validator.Validate(request); if (!validationResult.IsValid) { throw new EntityValidationException(nameof(UpdateUserInfo), request, validationResult.Errors); } User user = await _mainDbContext.Users .FirstOrDefaultAsync(p => p.Id == request.UserId, cancellationToken); if (user == null) { throw new EntityNotFoundException(nameof(User), request.UserId); } if (!string.IsNullOrEmpty(request.NewPassword) && !string.IsNullOrEmpty(request.CurrentPassword)) { bool isCurrentPasswordValid = BCrypt.Net.BCrypt.Verify(request.CurrentPassword, user.Password); if (!isCurrentPasswordValid) { throw new InvalidCredentialException(); } user.Password = BCrypt.Net.BCrypt.HashPassword(request.NewPassword); } user.FirstName = request.FirstName; user.LastName = request.LastName; await _mainDbContext.SaveChangesAsync(cancellationToken); return(_mapper.Map <UserDto>(user)); }
public async Task <UserDto> Handle(CreateUser request, CancellationToken cancellationToken) { CreateUserValidator validator = new CreateUserValidator(); ValidationResult validationResult = await validator.ValidateAsync(request, cancellationToken); if (!validationResult.IsValid) { throw new EntityValidationException(nameof(CreateUser), request, validationResult.Errors); } User currentUser = await _mainDbContext.Users .AsNoTracking() .Where(u => u.Email == request.Email && !u.IsDeleted) .FirstOrDefaultAsync(cancellationToken); if (currentUser != null) { throw new EntityAlreadyExists(nameof(User), request.Email); } User newUser = _mapper.Map <User>(request); newUser.Password = BCrypt.Net.BCrypt.HashPassword(request.Password); await _mainDbContext.Users.AddAsync(newUser, cancellationToken); await _mainDbContext.SaveChangesAsync(cancellationToken); _logger.LogDebug($"Created user: {request.Email}"); return(_mapper.Map <UserDto>(newUser)); }
public async Task <int> Handle(CreateUserCommand request, CancellationToken cancellationToken) { Nexawo.Domain.Entities.Main.User user = _mapper.Map <Nexawo.Domain.Entities.Main.User>(request.UserDTO); _context.Users.Add(user); int result = await _context.SaveChangesAsync(cancellationToken); return(result); }
public async Task <int> Handle(CreateReferralCommand request, CancellationToken cancellationToken) { Nexawo.Domain.Entities.Main.Referral referral = _mapper.Map <Nexawo.Domain.Entities.Main.Referral>(request.ReferralDTO); _context.Referrals.Add(referral); int result = await _context.SaveChangesAsync(cancellationToken); return(result); }
public async Task <int> Handle(CreateServiceCommand request, CancellationToken cancellationToken) { var service = _mapper.Map <Service>(request.ServiceDTO); _context.Services.Add(service); int result = await _context.SaveChangesAsync(cancellationToken); return(result); }
public async Task <LearningProgram> Add(LearningProgram param) { var entity = _mapper.Map <LearningProgramModel>(param); var result = await _context.LearningProgram.AddAsync(entity); await _context.SaveChangesAsync(); return(_mapper.Map <LearningProgram>(result.Entity)); }
public async Task <int> Handle(CreateEmployeeCommand request, CancellationToken cancellationToken) { var employee = _mapper.Map <Nexawo.Domain.Entities.Main.Employee>(request.EmployeeDTO); _context.Employees.Add(employee); int result = await _context.SaveChangesAsync(cancellationToken); return(result); }
public async Task <int> Create(IFormFile file, CancellationToken cancellationToken) { using (var stream = new MemoryStream()) { await file.CopyToAsync(stream); var entity = new Document() { Name = WebUtility.HtmlEncode(file.FileName), Content = stream.ToArray(), FileSize = file.Length }; _dbContext.AddEntity <Document>(entity); await _dbContext.SaveChangesAsync(cancellationToken); return(entity.DocumentId); } }
public async Task <Unit> Handle(DeleteEmployeeCommand request, CancellationToken cancellationToken) { var Entity = await _context.Employees.FindAsync(request.EmployeeId); if (Entity == null) { throw new NotFoundException(nameof(Employees), request.EmployeeId); } _context.Employees.Remove(Entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Subject> Update(Subject param) { var tracker = _context .Subject .Update(_mapper.Map <SubjectModel>(param)); await _context.SaveChangesAsync(); var entity = await _context .Subject .Include(x => x.Supervisor) .FirstOrDefaultAsync(x => x.Id == tracker.Entity.Id); return(_mapper.Map <Subject>(entity)); }
public async Task <Unit> Handle(UpdateTenantCommand request, CancellationToken cancellationToken) { var entity = await _context.Tenants.FindAsync(request.TenantId); if (entity == null) { throw new NotFoundException(nameof(Tenants), request.TenantId); } entity.TenantId = request.TenantId; entity.Name = request.Name; entity.DBConnection = request.DBConnection; await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <int> Handle(CreateTenantCommand request, CancellationToken cancellationToken) { var entity = new Tenant { TenantId = request.TenantId, Name = request.Name, DBConnection = request.DBConnection }; _context.Tenants.Add(entity); await _context.SaveChangesAsync(cancellationToken); await _mediator.Publish(new TenantCreated { TenantId = entity.TenantId }, cancellationToken); return(1); }
public async Task <bool> Handle(CreateViewCommand request, CancellationToken cancellationToken) { bool isSuccess = await _dynamicViewService.GenerateFile("Views", request.DynamicObjectDTO); if (isSuccess) { isSuccess = await _dynamicViewService.AddContent("Views", request.DynamicObjectDTO); if (isSuccess) { ServiceDetail serviceDetail = GenerateServiceDetailObject(request.DynamicObjectDTO); _context.ServiceDetails.Add(serviceDetail); await _context.SaveChangesAsync(cancellationToken); } } return(isSuccess); }
public async Task <Unit> Handle(DeleteTenantCommand request, CancellationToken cancellationToken) { var entity = await _context.Tenants.FindAsync(request.Id); if (entity == null) { throw new NotFoundException(nameof(Tenants), request.Id); } //Un Comment below code if tenant users are created and user want to delete tenant it won't allow user to delete tenant //var hasOrders = _context.TenantUsers.Any(od => od.ProductId == entity.ProductId); //if (hasOrders) //{ // // TODO: Add functional test for this behaviour. // throw new DeleteFailureException(nameof(Product), request.Id, "There are existing orders associated with this product."); //} _context.Tenants.Remove(entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(UpdateEmployeeCommand request, CancellationToken cancellationToken) { var Entity = await _context.Employees.FindAsync(request.EmployeeId); if (Entity == null) { throw new NotFoundException(nameof(Employees), request.EmployeeId); } Entity.EmployeeId = request.EmployeeId; Entity.FirstName = request.FirstName; Entity.LastName = request.LastName; Entity.CNIC = request.CNIC; Entity.Email = request.Email; Entity.Age = request.Age; Entity.Address = request.Address; await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); //Entity.EmployeeId = request.EmployeeId; }
public async Task <UserDto> Handle(DeleteUser request, CancellationToken cancellationToken) { DeleteUserValidator validator = new DeleteUserValidator(); ValidationResult validationResult = await validator.ValidateAsync(request, cancellationToken); if (!validationResult.IsValid) { throw new EntityValidationException(nameof(DeleteUser), request.UserId, validationResult.Errors); } User user = await _mainDbContext.Users .FirstOrDefaultAsync(u => u.Id == request.UserId, cancellationToken); if (user.Equals(null)) { throw new EntityNotFoundException(nameof(User), request.UserId); } user.IsDeleted = true; await _mainDbContext.SaveChangesAsync(cancellationToken); return(_mapper.Map <UserDto>(user)); }