Esempio n. 1
0
        public async Task <ICommandResult <Guid> > Handle(AddDimensionTravelledInputModel request, CancellationToken cancellationToken)
        {
            if (await _repositoryRick.FindById(request.idRick) == null)
            {
                return(new CommandResult <Guid>
                {
                    message = "Ops! Esse rick não existe.",
                    success = false,
                    httpStatusCode = HttpStatusCode.NotFound
                });
            }

            if (await _repositoryDimension.FindById(request.idDimension) == null)
            {
                return(new CommandResult <Guid>
                {
                    message = "Ops! Essa dimensão não existe.",
                    success = false,
                    httpStatusCode = HttpStatusCode.NotFound
                });
            }

            DimensionTravelled lastDimensionTravelled = await _repositoryDimensionTravelled.FindLastTravel(request.idRick);

            if (lastDimensionTravelled != null && lastDimensionTravelled.IdDimensionCurrent == request.idDimension)
            {
                return(new CommandResult <Guid> {
                    message = "Ops! Você não pode viajar para a mesma dimensão que você está, selecione outra.",
                    success = false,
                    httpStatusCode = HttpStatusCode.BadRequest
                });
            }

            DimensionTravelled dimensionTravelled = _mapper.Map <AddDimensionTravelledInputModel, DimensionTravelled>(request);

            dimensionTravelled.IdDimensionBefore = (lastDimensionTravelled != null ? lastDimensionTravelled.IdDimensionCurrent : null);
            await _repositoryDimensionTravelled.Insert(dimensionTravelled);

            await _unitOfWork.Commit();

            return(new CommandResult <Guid>
            {
                response = dimensionTravelled.Id,
                message = $"Viagem criada com sucesso!",
                success = true,
                httpStatusCode = HttpStatusCode.Created
            });
        }