コード例 #1
0
        public async Task <TicketArchive> PutTicketAsync(int ticketId, TicketArchive ticketArchive)
        {
            if (ticketId != ticketArchive.Id)
            {
                throw new Exception("ticketId is not equal to ticketArchive Id.");
            }

            //_context.Entry(ticketArchive).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_context.TicketArchives.Any(e => e.Id == ticketId))
                {
                    throw new Exception("ticket with this Id does not exist.");
                }
                else
                {
                    throw;
                }
            }
            return(ticketArchive);
        }
コード例 #2
0
        public async Task <TicketArchive> BuyTicketAsync(int railwayId)
        {
            var railway = _context.Railways.Where(c => c.Id == railwayId).FirstOrDefault();

            if (railway == null)
            {
                throw new Exception("railway with this ID does not exist.");
            }
            if (railway.FreePlacesAmount > 0)
            {
                railway.FreePlacesAmount = railway.FreePlacesAmount - 1;
                //_context.Entry(railway).State = EntityState.Modified;

                var currentId   = _httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == "id")?.Value;
                var currentUser = await _userManager.FindByIdAsync(currentId);

                var newTicketArchive = new TicketArchive
                {
                    OwnerId       = currentUser.Id,
                    StartPoint    = railway.StartPoint,
                    EndPoint      = railway.EndPoint,
                    DepartureDate = railway.DepartureDate
                };
                await _context.TicketArchives.AddAsync(newTicketArchive);

                await _context.SaveChangesAsync();

                return(newTicketArchive);
            }

            throw new Exception("no more tickets.");
        }
コード例 #3
0
        public async Task <IActionResult> PutTicket(int ticketId, [FromBody] TicketArchive ticket)
        {
            try
            {
                var result = await _ticketArciveService.PutTicketAsync(ticketId, ticket);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                return(BadRequest("Error: " + ex.Message));
            }
        }
コード例 #4
0
 public async Task PostTicketAsync(TicketArchive ticketArchive)
 {
     _context.TicketArchives.Add(ticketArchive);
     await _context.SaveChangesAsync();
 }
コード例 #5
0
        public async Task <ActionResult <TicketArchive> > PostTicket([FromBody] TicketArchive ticket)
        {
            await _ticketArciveService.PostTicketAsync(ticket);

            return(CreatedAtAction(nameof(GetTicket), new { ticketId = ticket.Id }, ticket));
        }