Beispiel #1
0
        public async Task <Result <BikeResponse> > UpdateBikeAsync(UpdateBikeCommand request)
        {
            var bikeResult = await _bikeRepository.GetBikeAsync(request.Id);

            BikeResponse bikeInDb = new BikeResponse();

            bikeResult.Match(s => bikeInDb = s,
                             f => new Result <BikeResponse>(f));

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

            // validation if bike is being added to the specific HomeBase ...
            if (request.HomeBaseId is not null && bikeInDb.HomeBaseId != request.HomeBaseId)
            {
                if (!_homeBaseService.CheckIfExistsAsync(request.HomeBaseId))
                {
                    return(new Result <BikeResponse>(new BadRequestException(Error.HomeBaseNotFound)));
                }

                if (!await _homeBaseService.CheckIfFreeSlotsAsync(request.HomeBaseId))
                {
                    return(new Result <BikeResponse>(new BadRequestException(Error.HomeBaseFull)));
                }
            }

            return(await _bikeRepository.UpdateBikeAsync(request));
        }
Beispiel #2
0
        public async Task <Result <BikeResponse> > UpdateBikeAsync(UpdateBikeCommand request)
        {
            var bikeInDb = await _dbContext.Bikes.FindAsync(request.Id);

            if (bikeInDb is null)
            {
                return(new Result <BikeResponse>(new InternalErrorException(Error.BikeNotFound)));
            }

            bikeInDb = _mapper.Map(request, bikeInDb);

            _dbContext.Update(bikeInDb);

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

            return(new Result <BikeResponse>(_mapper.Map <BikeResponse>(bikeInDb)));
        }
Beispiel #3
0
        public async Task <IActionResult> Put([FromBody] UpdateBikeCommand command)
        {
            return((await _mediator.Send(command)).Match <IActionResult>(
                       s => Ok(s),
                       f =>
            {
                if (f is BadRequestException)
                {
                    return BadRequest(f.Message);
                }

                return StatusCode(StatusCodes.Status500InternalServerError);
            }));
        }
Beispiel #4
0
        public async Task <Result <BikeResponse> > Handle(UpdateBikeCommand request, CancellationToken cancellationToken)
        {
            var result = await _bikeService.UpdateBikeAsync(request);

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

                _client.PublishEvent(message);
            });

            return(result);
        }