Exemplo n.º 1
0
        public async Task <IActionResult> Put([FromBody] UpdateHomeBaseCommand command)
        {
            var result = await _mediator.Send(command);

            return(result.Match <IActionResult>(s => Ok(s),
                                                f =>
            {
                if (f is BadRequestException)
                {
                    return BadRequest(f.Message);
                }

                return StatusCode(StatusCodes.Status500InternalServerError);
            }));
        }
Exemplo n.º 2
0
        public async Task <Result <HomeBaseResponse> > Handle(UpdateHomeBaseCommand request, CancellationToken cancellationToken)
        {
            var result = await _homeBaseService.UpdateHomeBaseAsync(request);

            result.IfSucc(s =>
            {
                var message = new HomeBasePostMessage()
                {
                    MessageType = s.GetType().Name,
                    Method      = ApiMethod.PUT.ToString(),
                    Message     = s
                };

                _client.PublishEvent(message);
            });

            return(result);
        }
Exemplo n.º 3
0
        public async Task <Result <HomeBaseResponse> > UpdateHomeBaseAsync(UpdateHomeBaseCommand request)
        {
            var baseToUpdate = await _dbContext.HomeBases.FindAsync(request.Id);

            var copy = baseToUpdate;

            baseToUpdate = _mapper.Map <UpdateHomeBaseCommand, HomeBase>(request, baseToUpdate);

            _dbContext.Update(baseToUpdate);

            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(new Result <HomeBaseResponse>(new InternalErrorException(Error.ErrorWhileProcessingOperation)));
            }

            return(new Result <HomeBaseResponse>(_mapper.Map <HomeBaseResponse>(baseToUpdate)));
        }
Exemplo n.º 4
0
        public async Task <Result <HomeBaseResponse> > UpdateHomeBaseAsync(UpdateHomeBaseCommand request)
        {
            var homeBaseResponse = new HomeBaseResponse();

            var result = (await GetHomeBaseAsync(request.Id))
                         .Match(s => homeBaseResponse = s,
                                f => new Result <HomeBaseResponse>(f));

            if (result.IsFaulted)
            {
                return(result);
            }

            if (homeBaseResponse.Name != request.Name)
            {
                if (!await CheckIfUniqueAsync(request.Name))
                {
                    return(new Result <HomeBaseResponse>(new BadRequestException(Error.InvalidHomeBaseName)));
                }
            }

            return(await _homeBaseRepository.UpdateHomeBaseAsync(request));
        }