Пример #1
0
        public ActionResult <Ticket> CreateTicket(Ticket model)
        {
            if (model.Id > 0)
            {
                return(BadRequest(
                           new ProblemDetails {
                    Detail = "This method can't be used to update tickets."
                }));
            }
            var evt = _context.Events.Find(model.EventId);

            if (evt == null)
            {
                return(BadRequest(
                           new ProblemDetails {
                    Detail = $"There's no event with id {model.EventId}."
                }));
            }
            var entity = new DataAccess.Models.Ticket();

            _mapper.Map(model, entity);
            entity.TicketGuid   = Guid.NewGuid();
            entity.TicketNumber = entity.TicketNumber
                                  ?? TicketNumberHelper.GenerateTicketNumber(evt);
            SetAuthorInfo(entity);
            _context.Add(entity);
            _context.SaveChanges();
            model = _mapper.Map <Ticket>(entity);
            return(CreatedAtAction(nameof(GetById), new { id = model.Id }, model));
        }
Пример #2
0
        public IActionResult ValidateTicket(string id)
        {
            Guid ticketGuid;

            if (!Guid.TryParse(id, out ticketGuid))
            {
                _logger.LogInformation("The parameter id is no valid guid.");
                return(TicketNotFound());
            }
            DataAccess.Models.Ticket ticket =
                _context.Tickets
                .Include(e => e.Event)
                .Include(e => e.TicketType)
                .SingleOrDefault(e => e.TicketGuid == ticketGuid);
            if (ticket == null)
            {
                _logger.LogInformation("Ticket with id {id} was not found in the database.", ticketGuid);
                return(TicketNotFound());
            }
            if (ticket.Validated)
            {
                _logger.LogInformation("The ticket has been already used before.");
                return(View("TicketUsed", ticket));
            }
            ticket.Validated = true;
            _context.SaveChanges();
            return(View("TicketValid", ticket));
        }
Пример #3
0
        private void SetAuthorInfo(DataAccess.Models.Ticket entity)
        {
            var timestamp     = DateTime.UtcNow;
            int currentUserId = User.GetUserId();

            entity.EditedAt = timestamp;
            entity.EditorId = currentUserId;
            if (entity.Id <= 0)
            {
                entity.CreatorId = currentUserId;
                entity.CreatedAt = timestamp;
            }
        }