Пример #1
0
        /// <summary>
        /// updates a building by a dto
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task <AlpApiResponse> UpdateBuilding(BuildingDto dto)
        {
            var response = new AlpApiResponse();

            try
            {
                _logger.LogDebug(new
                {
                    action = nameof(UpdateBuilding),
                    dto    = dto?.ToString()
                }.ToString());

                dto.Validate();

                var updatedEntity = await _context.Building.Include(building => building.Location).FirstOrDefaultAsync(building => building.BuildingId == 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);
        }
Пример #2
0
        /// <summary>
        /// adds a new building into the database
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task <AlpApiResponse <BuildingDto> > InsertNewBuilding(BuildingDto dto)
        {
            var response = new AlpApiResponse <BuildingDto>();

            try
            {
                _logger.LogDebug(new
                {
                    action = nameof(InsertNewBuilding),
                    dto    = dto?.ToString()
                }.ToString());

                dto.Validate();

                var entity = dto.DtoToEntity();
                entity.Location = null;
                await _context.Building.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);
        }