Ejemplo n.º 1
0
        public async Task <Result> UpdateEmployee(long id, PutEmployeeDTO dto)
        {
            var employee = await this.dbContext.Employees
                           .Where(e => e.Id == id)
                           .Include(e => e.Contacts).SingleOrDefaultAsync();

            if (employee == null)
            {
                return(Result.Failure($"Employee with id {id} not found"));
            }

            employee.Name         = dto.Name;
            employee.Surname      = dto.Surname;
            employee.Patronymic   = dto.Patronymic;
            employee.BirthDate    = dto.BirthDate;
            employee.Organization = dto.Organization;
            employee.Position     = dto.Position;

            var contacts = this.GetContactsFromDTO(dto.Contacts);

            if (contacts.IsFailure)
            {
                return(Result.Failure($"Can't update contacts due to errors: {contacts.Error}"));
            }

            employee.ClearContacts();
            employee.AddContacts(contacts.Value);
            await this.dbContext.SaveChangesAsync();

            return(Result.Success());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> UpdateEmployee(long id, [FromBody] PutEmployeeDTO dto)
        {
            var result = await this.employeesService.UpdateEmployee(id, dto);

            return(result.IsSuccess ? Ok() : Error(result.Error));
        }