Ejemplo n.º 1
0
        public async Task <ActionResult <Trip> > PostTrip(TripReqDTO tripDTO)
        {
            try
            {
                var trip = await _tripService.CreateTrip(tripDTO);

                return(CreatedAtAction("PostTrip", trip));
            }
            catch (InvalidOperationException exp)
            {
                return(BadRequest(exp.Message));
            }
        }
Ejemplo n.º 2
0
        public async Task <TripResDTO> CreateTrip(TripReqDTO tripDTO)
        {
            var tripExists = await ActiveTripForUserExists(tripDTO.UserId);

            if (tripExists)
            {
                throw new InvalidOperationException("There is already an active trip for user");
            }

            var trip = new Trip()
            {
                IsFinished = false,
                StartTime  = DateTime.Now,
                TramId     = tripDTO.TramId,
                UserId     = tripDTO.UserId,
                Length     = 0f
            };
            var tr = await _context.Trips.AddAsync(trip);

            await _context.SaveChangesAsync();

            return(_mapper.Map <TripResDTO>(tr.Entity));
        }
Ejemplo n.º 3
0
        public async Task <TripResDTO> CreateTrip(TripReqDTO tripDTO)
        {
            var userId     = Int32.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
            var tripExists = await ActiveTripForUserExists(userId);

            if (tripExists)
            {
                throw new InvalidOperationException("There is already an active trip for user");
            }

            var trip = new Trip()
            {
                IsFinished = false,
                StartTime  = DateTime.Now,
                TramId     = tripDTO.TramId,
                UserId     = userId,
                Length     = 0f
            };
            var tr = await _context.Trips.AddAsync(trip);

            await _context.SaveChangesAsync();

            return(_mapper.Map <TripResDTO>(tr.Entity));
        }