예제 #1
0
        public async Task <Result <HomeBaseResponse> > AddHomeBaseAsync(AddHomeBaseCommand request)
        {
            HomeBase baseToAdd = new HomeBase();

            try
            {
                baseToAdd = _mapper.Map <HomeBase>(request);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(new Result <HomeBaseResponse>(e));
            }

            _dbContext.Add(baseToAdd);

            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>(baseToAdd)));
        }
예제 #2
0
        public async Task <Result <HomeBaseResponse> > AddHomeBaseAsync(AddHomeBaseCommand request)
        {
            if (!await CheckIfUniqueAsync(request.Name))
            {
                return(new Result <HomeBaseResponse>(new BadRequestException(Error.InvalidHomeBaseName)));
            }

            return(await _homeBaseRepository.AddHomeBaseAsync(request));
        }
예제 #3
0
        public async Task <IActionResult> Post([FromBody] AddHomeBaseCommand 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);
            }));
        }
예제 #4
0
        public async Task <Result <HomeBaseResponse> > Handle(AddHomeBaseCommand request, CancellationToken cancellationToken)
        {
            var result = await _homeBaseService.AddHomeBaseAsync(request);

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

                _client.PublishEvent(message);
            });

            return(result);
        }