/// <summary> /// adds a new section to the database /// </summary> /// <param name="dto"></param> /// <returns></returns> public async Task <AlpApiResponse <SectionDto> > InsertNewSection(SectionDto dto) { var response = new AlpApiResponse <SectionDto>(); try { _logger.LogDebug(new { action = nameof(InsertNewSection), dto = dto?.ToString() }.ToString()); dto.Validate(); var entity = dto.DtoToEntity(); entity.Floor = null; entity.Department = null; await _context.Section.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.Message = e.Message; response.Success = false; } return(response); }
/// <summary> /// updates a section by dto /// </summary> /// <param name="dto"></param> /// <returns></returns> public async Task <AlpApiResponse> UpdateSection(SectionDto dto) { var response = new AlpApiResponse(); try { _logger.LogDebug(new { action = nameof(UpdateSection), dto = dto?.ToString() }.ToString()); dto.Validate(); var updatedEntity = await _context.Section .Include(section => section.Floor) .Include(section => section.Department) .FirstOrDefaultAsync(section => section.SectionId == 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.Message = e.Message; response.Success = false; } return(response); }