public async Task <IActionResult> Post([FromBody] Entry entry) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.Entries.Add(entry); await _context.SaveChangesAsync(); return(CreatedAtAction("GetEntry", new { id = entry.Id }, entry)); }
public async Task <Person> AddPerson(Person person) { using (var context = new PhoneBookDbContext(_dbConfiguration)) { person.Id = Guid.NewGuid(); await context.People.AddAsync(person); await context.SaveChangesAsync(); return(person); } }
protected override async Task Handle(DeletePersonInformationCommand command, CancellationToken cancellationToken) { var personInformation = await _dbContext.Informations.FindAsync(command.InformationId); if (personInformation != null) { _dbContext.Informations.Remove(personInformation); await _dbContext.SaveChangesAsync(); } _logger.LogInformation($"[Local Transaction] : Person information deleted."); await _busPublisher.PublishAsync(new PersonInformationDeleted(personInformation.Id, personInformation.Info), null); }
protected override async Task Handle(DeletePersonEmailCommand command, CancellationToken cancellationToken) { var personEmail = await _dbContext.Emails.FindAsync(command.EmailId); if (personEmail != null) { _dbContext.Emails.Remove(personEmail); await _dbContext.SaveChangesAsync(); } _logger.LogInformation($"[Local Transaction] : Person email deleted."); await _busPublisher.PublishAsync(new PersonEmailDeleted(personEmail.Id, personEmail.EmailAdress), null); }
protected override async Task Handle(DeletePersonPhoneNumberCommand command, CancellationToken cancellationToken) { var personPhoneNumber = await _dbContext.Phones.FindAsync(command.PhoneNumberId); if (personPhoneNumber != null) { _dbContext.Phones.Remove(personPhoneNumber); await _dbContext.SaveChangesAsync(); } _logger.LogInformation($"[Local Transaction] : Person phone number deleted."); await _busPublisher.PublishAsync(new PersonPhoneNumberDeleted(personPhoneNumber.Id, personPhoneNumber.PhoneNumber), null); }
public async Task UpdatePerson(Person person) { using (var context = new PhoneBookDbContext(_dbConfiguration)) { var entity = await context.People.FirstOrDefaultAsync(p => p.Id == person.Id); entity.FirstName = person.FirstName; entity.LastName = person.LastName; entity.Phone = person.Phone; context.People.Update(entity); await context.SaveChangesAsync(); } }
public async Task <Result <Employee> > Handle(UpdateEmployeeCommand request, CancellationToken cancellationToken) { var result = _validator.Validate(request, ruleSet: "PreValidationEmployeeUpdate"); if (result.Errors.Count > 0) { return(Result.Failure <Employee>(result.Errors.First().ErrorMessage)); } var employeeDb = _mapper.Map <EmployeeDb>(request); _context.Entry(employeeDb).State = EntityState.Modified; await _context.SaveChangesAsync(cancellationToken); return(Result.Ok <Employee>(_mapper.Map <Employee>(employeeDb))); }
public async Task <Result <Employee> > Handle(CreateEmployeeCommand request, CancellationToken cancellationToken) { var result = _validator.Validate(request, ruleSet: "CheckExistingEmployeeValidation"); if (result.Errors.Count > 0) { return(Result.Failure <Employee>(result.Errors.First().ErrorMessage)); } var employeeDb = _mapper.Map <EmployeeDb>(request); _context.Add(employeeDb); await _context.SaveChangesAsync(cancellationToken); return(Result.Ok <Employee>(_mapper.Map <Employee>(employeeDb))); }
public async Task <bool> Handle(DeleteFavoritesByIdCommand request, CancellationToken cancellationToken) { var deletedFavorites = _context.Favorites .Where(x => x.Id == request.Id) .FirstOrDefault(); if (deletedFavorites == null) { _logger.LogError($"There is not a favorites with the Id '{request.Id}'..."); return(await Task.FromResult(false)); } _context.Remove(deletedFavorites); await _context.SaveChangesAsync(cancellationToken); return(await Task.FromResult(true)); }
public async Task DeletePerson(Guid personId) { using (var context = new PhoneBookDbContext(_dbConfiguration)) { var exists = PersonExists(personId); if (exists) { var person = await GetPersonById(personId); if (person != null) { context.People.Remove(person); await context.SaveChangesAsync(); } } } }
protected override async Task Handle(CreatePersonCommand command, CancellationToken cancellationToken) { _dbContext.Persons.Add(new Data.Entity.Person { Id = command.Id, Name = command.Name, Surname = command.Surname, CompanyName = command.CompanyName, }); await _dbContext.SaveChangesAsync(); _logger.LogInformation($"[Local Transaction] : Person created."); await _busPublisher.PublishAsync(new PersonCreated(command.Id, command.Name, command.Surname, command.CompanyName), null); }
protected override async Task Handle(CreatePersonEmailCommand command, CancellationToken cancellationToken) { var person = await _dbContext.Persons.FindAsync(command.PersonId); if (person != null) { _dbContext.Emails.Add(new Data.Entity.Email { Id = command.Id, PersonId = person.Id, EmailAdress = command.EmailAdress }); await _dbContext.SaveChangesAsync(); } _logger.LogInformation($"[Local Transaction] : Person email created."); await _busPublisher.PublishAsync(new PersonEmailCreated(person.Id, person.Name), null); }
public async Task <Result <Favorites> > Handle(CreateFavoritesCommand request, CancellationToken cancellationToken) { var result = _validator.Validate(request, ruleSet: "CheckExistingEmployeeIdValidation"); if (result.Errors.Count > 0) { return(Result.Failure <Favorites>(result.Errors.First().ErrorMessage)); } var favoriteDb = _mapper.Map <FavoritesDb>(request); var employee = await _context.Employees .Where(x => x.Id == request.EmployeeId) .Select(b => b) .FirstOrDefaultAsync(); favoriteDb.WorkerDb = employee; _context.Add(favoriteDb); await _context.SaveChangesAsync(cancellationToken); return(Result.Ok <Favorites>(_mapper.Map <Favorites>(favoriteDb))); }
public async Task <bool> Handle(DeleteEmployeeByIdCommand request, CancellationToken cancellationToken) { var deletedEmployee = _context.Employees .Where(x => x.Id == request.Id) .FirstOrDefault(); if (deletedEmployee == null) { _logger.LogError($"There is not a employee with the Id '{request.Id}'..."); return(await Task.FromResult(false)); } var deletedFavorites = _context.Favorites .Include(e => e.WorkerDb) .Where(x => x.WorkerDb.Id == request.Id) .FirstOrDefault(); _context.Remove(deletedEmployee); _context.Remove(deletedFavorites); await _context.SaveChangesAsync(cancellationToken); return(await Task.FromResult(true)); }
protected override async Task Handle(DeletePersonCommand command, CancellationToken cancellationToken) { var person = await _dbContext.Persons.Include(p => p.Informations) .Include(p => p.Locations) .Include(p => p.Phones) .Include(p => p.Emails).FirstOrDefaultAsync(s => s.Id == command.PersonId); if (person != null) { _dbContext.Emails.RemoveRange(person.Emails); _dbContext.Informations.RemoveRange(person.Informations); _dbContext.Phones.RemoveRange(person.Phones); _dbContext.Locations.RemoveRange(person.Locations); _dbContext.Persons.Remove(person); await _dbContext.SaveChangesAsync(); } _logger.LogInformation($"[Local Transaction] : Person deleted."); await _busPublisher.PublishAsync(new PersonDeleted(person.Id, person.Name, person.Surname, person.CompanyName), null); }
public async Task <int> CompleteAsync() { return(await _context.SaveChangesAsync()); }
public async Task <Maybe <IEnumerable <Employee> > > Handle(ImportEmployeeByFileCommand request, CancellationToken cancellationToken) { List <Employee> list = new List <Employee>(); if (request.EmployeeDelete != null) { Department department = new Department(); List <string> listString = new List <string>(); using (StreamReader reader = new StreamReader(request.EmployeeDelete.OpenReadStream(), System.Text.Encoding.Default)) { while (reader.Peek() >= 0) { listString.Add(await reader.ReadLineAsync()); } } foreach (string line in listString) { try { department = new Department(); string[] mass = line.Split(';'); department = await _context.Departments .Where(x => x.Name == mass[1]) .Select(d => _mapper.Map <Department>(d)) .FirstOrDefaultAsync(); if (department != null) { var deletedEmployee = await _context.Employees .Where(x => x.Name_Upper.Equals(mass[0].ToUpper()) && x.DepartmentDbId.Equals(department.Id)) .Select(d => d) .FirstOrDefaultAsync(); if (deletedEmployee != null) { _context.Remove(deletedEmployee); } if (deletedEmployee == null || department == null) { Employee employee = new Employee(); employee.Name = mass[0]; employee.Position = mass[1]; employee.Telephone = "Сотрудник не найден, удаление не завершено"; list.Add(employee); _logger.LogError($"There is not a employee with the Name '{mass[0]}' and Position '{mass[1]}'..."); } } } catch (Exception exc) { Employee employee = new Employee(); employee.Name = line; employee.Telephone = exc.Message + " удаление не завершено"; list.Add(employee); } } await _context.SaveChangesAsync(cancellationToken); //save file /*string webRootPath = _hostEnvironment.ContentRootPath; * var newPath = Path.Combine(webRootPath, folderName); * if (!Directory.Exists(newPath)) * { * Directory.CreateDirectory(newPath); * } * fileNameDelete = new String(Path.GetFileNameWithoutExtension(postedDelete.FileName).Take(10).ToArray()).Replace(" ", "-"); * fileNameDelete = fileNameDelete + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedDelete.FileName); * string fullPath = Path.Combine(newPath, fileNameDelete); * using (var stream = new FileStream(fullPath, FileMode.Create)) * { * postedDelete.CopyTo(stream); * }*/ } if (request.EmployeeNew != null) { Employee employee = new Employee(); Department department = new Department(); List <string> listString = new List <string>(); using (StreamReader reader = new StreamReader(request.EmployeeNew.OpenReadStream(), System.Text.Encoding.Default)) { while (reader.Peek() >= 0) { listString.Add(await reader.ReadLineAsync()); } } foreach (string line in listString) { try { employee = new Employee(); department = new Department(); string[] mass = line.Split(';'); employee.Name = mass[0]; employee.Position = mass[2]; var choosenEmployee = await _context.Employees .Where(x => x.Name_Upper.Equals(mass[0].ToUpper()) && x.Position_Upper.Equals(mass[2].ToUpper())) .FirstOrDefaultAsync(); if (choosenEmployee != null) { employee.Telephone = "Сотрудник уже существует, импорт не завершен"; list.Add(employee); } else { department = await _context.Departments .Where(x => x.Name == mass[1]) .Select(d => _mapper.Map <Department>(d)) .FirstOrDefaultAsync(); if (department != null) { employee.DepartmentId = department.Id; var employeeDb = _mapper.Map <EmployeeDb>(employee); _context.Add(employeeDb); } else { employee.Telephone = "Не найден отдел, импорт не завершен"; list.Add(employee); } } } catch (Exception exc) { employee.Name = line; employee.Telephone = exc.Message + " импорт не завершен"; list.Add(employee); } } await _context.SaveChangesAsync(cancellationToken); } return(list != null ? Maybe <IEnumerable <Employee> > .From(list) : Maybe <IEnumerable <Employee> > .None); }