/// <summary> /// updates a department by dto /// </summary> /// <param name="dto"></param> /// <returns></returns> public async Task <AlpApiResponse> UpdateDepartment(DepartmentDto dto) { var response = new AlpApiResponse(); try { _logger.LogDebug(new { action = nameof(UpdateDepartment), dto = dto?.ToString() }.ToString()); dto.Validate(); var updatedEntity = await _context.Department.Include(department => department.Employee).FirstOrDefaultAsync(Department => Department.DepartmentId == dto.Id); updatedEntity.UpdateEntityByDto(dto); await _context.SaveChangesAsync(); } catch (Exception e) { _logger.LogError(new { exception = e, message = e.Message, innerException = e, innerExceptionMessage = e.InnerException?.Message }.ToString()); response.Success = false; response.Message = e.Message; } return(response); }
/// <summary> /// adds a new department to the database /// </summary> /// <param name="dto"></param> /// <returns></returns> public async Task <AlpApiResponse <DepartmentDto> > InsertNewDepartment(DepartmentDto dto) { var response = new AlpApiResponse <DepartmentDto>(); try { _logger.LogDebug(new { action = nameof(InsertNewDepartment), dto = dto?.ToString() }.ToString()); dto.Validate(); var entity = dto.DtoToEntity(); entity.Employee = null; await _context.Department.AddAsync(entity); await _context.SaveChangesAsync(); response.Value = entity.EntityToDto(); } catch (Exception e) { _logger.LogError(new { exception = e, message = e.Message, innerException = e, innerExceptionMessage = e.InnerException?.Message }.ToString()); response.Success = false; response.Message = e.Message; } return(response); }