Ejemplo n.º 1
0
        public async Task <ServiceResponse> AddCounter(CounterAddModel model)
        {
            var home = await _context.Houses.FirstOrDefaultAsync(x => x.Id == model.HouseId);

            if (home == null)
            {
                return(new ServiceResponse()
                {
                    Result = $"House id {model.HouseId} not found",
                    Status = ServiceResponseStatus.NotFound
                });
            }

            _logger.LogInformation(@$ "{_serviceName} Add {nameof(Counter)}", model);
            var counter = _mapperService.Mapper.Map <CounterAddModel, Counter>(model);

            _context.Counters.Add(counter);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception exception)
            {
                return(HandleException.HandleSqlException(exception));
            }

            _logger.LogInformation(@$ "{_serviceName} Add {nameof(Counter)} complete", model);

            return(new ServiceResponse()
            {
                Status = ServiceResponseStatus.Ok,
                Result = _mapperService.Mapper.Map <Counter, CounterViewModel>(counter)
            });
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <CounterViewModel> > AddCounter(CounterAddModel model)
        {
            var response = await _service.AddCounter(model);

            if (response.Status == ServiceResponseStatus.NotFound)
            {
                return(NotFound(response.Result));
            }

            if (response.Status == ServiceResponseStatus.Ok && response.Result is CounterViewModel result)
            {
                return(result);
            }

            return(BadRequest(response.Result));
        }