public async Task <IActionResult> Ticket(CreateTicketBindingModel createTicketBindingModel) { //get competitor ID IEnumerable <Competitor> competitors = await new CompetitorQueryHandler().HandleAsync(new CompetitorQuery { Name = createTicketBindingModel.competitor }); Competitor competitor = competitors.Where(c => c.Name == createTicketBindingModel.competitor) .ToList() .FirstOrDefault(); if (competitor == null) { return(RedirectToAction(StaticConstants.Competition, StaticConstants.ApiCompetitions, new { name = createTicketBindingModel.competitionName, error = StaticConstants.TicketNotCreatedCreatorNullError })); } Guid competitorId = competitor.Id; //get gambler ID var currentGambler = await new GamblerQueryHandler().HandleAsync(new GamblerQuery { Name = createTicketBindingModel.gambler }); Gambler gambler = currentGambler.Where(c => c.Name == createTicketBindingModel.gambler) .ToList() .FirstOrDefault(); if (gambler == null) { return(RedirectToAction(StaticConstants.Competition, StaticConstants.ApiCompetitions, new { name = createTicketBindingModel.competitionName, error = StaticConstants.TicketNotCreatedGamblerNullError })); } Guid gamblerId = gambler.Id; //Create BuyTicketCommand command BuyTicketCommand command = new BuyTicketCommand { Id = Guid.NewGuid(), CompetitionId = Guid.Parse(createTicketBindingModel.competitionId), CompetitorId = competitorId, GamblerId = gamblerId, IsPaymentReceived = createTicketBindingModel.paymentReceived }; var handler = new BuyTicketHandler(); await handler.HandleAsync(command); return(RedirectToAction(StaticConstants.Ticket, new { id = command.Id, message = StaticConstants.TicketBoughtSuccessMessage })); }
public async Task <IActionResult> CreateTicket(CreateTicketBindingModel model) { if (ModelState.IsValid) { await _service.CreateTicket(model); return(RedirectToAction("Index", "Home")); } IEnumerable <Project> projects = _service.ProjectIdToSelect(); ViewBag.ProjectId = new SelectList(projects, "Id", "Id", model.ProjectId); return(View()); }
public async Task <ActionSummary> Buy(CreateTicketBindingModel model) { var projection = this.DbContext.Projections.FirstOrDefault(p => p.Id == model.ProjectionId); if (projection == null) { return(new ActionSummary(false, $"No projection with Id {model.ProjectionId}")); } if (projection.StartDate < DateTime.UtcNow) { return(new ActionSummary(false, $"You can't buy ticket for projection with start date {projection.StartDate}")); } var room = this.DbContext.Rooms.FirstOrDefault(r => r.Id == projection.RoomId); bool isValidRow = model.Row > 0 && model.Row <= room.Rows; bool isValidRColumn = model.Column > 0 && model.Column <= room.SeatsPerRow; if (!isValidRow || !isValidRColumn) { return(new ActionSummary(false, $"Invalid seat place")); } bool isReserved = this.DbContext.Reservations.Where(r => r.ProjectionId == model.ProjectionId) .FirstOrDefault(r => r.Row == model.Row && r.Column == model.Column) .Equals(null); bool isSold = this.DbContext.Tickets.Where(t => t.ProjectionId == model.ProjectionId) .FirstOrDefault(r => r.Row == model.Row && r.Column == model.Column) .Equals(null); if (isReserved || isSold) { return(new ActionSummary(false, $"The place is occupied")); } Ticket newTicket = new Ticket(); newTicket.ProjectionId = model.ProjectionId; newTicket.Row = model.Row; newTicket.Column = model.Column; this.DbContext.Tickets.Add(newTicket); int rowsAffected = await this.DbContext.SaveChangesAsync(); return(new ActionSummary(rowsAffected == 1)); }
public async Task <IActionResult> CreateTicket()//here the val i get User.Identity.Name { var user = await _service.FindUserIdByUserName(User.Identity.Name); var newTicket = new CreateTicketBindingModel { AuthorId = user.Id, Priority = Priority.Low }; IEnumerable <Project> projects = _service.ProjectIdToSelect(); ViewBag.ProjectId = new SelectList(projects, "Id", "Id", newTicket.ProjectId); return(View(newTicket)); }
public async Task <ActionSummary> BuyReserved(CreateTicketBindingModel model) { var reservation = this.DbContext.Reservations.FirstOrDefault(r => r.Id == model.ReservationId); if (reservation == null) { return(new ReservationSummary(false, $"No reservation with Id {model.ReservationId}")); } if (reservation.IsCanceled) { return(new ReservationSummary(false, $"Reservation is canceled")); } var hasTicketForReservation = this.DbContext.Tickets.Any(t => t.ReservationId == model.ReservationId); if (hasTicketForReservation) { return(new ReservationSummary(false, $"Reservation is already used")); } var projection = this.DbContext.Projections.First(p => p.Id == reservation.ProjectionId); if (projection.StartDate < DateTime.UtcNow.AddMinutes(MINUTES_BEFORE_PROJECTION)) { return(new ReservationSummary(false, $"To late for reservation for projection with start date {projection.StartDate}")); } Ticket newTicket = new Ticket(); newTicket.ProjectionId = projection.Id; newTicket.ReservationId = reservation.Id; newTicket.Row = model.Row; newTicket.Column = model.Column; int rowsAffected = await this.DbContext.SaveChangesAsync(); if (rowsAffected == 0) { return(new ReservationSummary(false, $"Ticket sell server error")); } projection.AvailableSeatsCount -= 1; return(new ActionSummary(true)); }
public async Task <IHttpActionResult> Buy(CreateTicketBindingModel model) { ActionSummary result; if (model.ReservationId == 0) { result = await this.service.Buy(model); } else { result = await this.service.BuyReserved(model); } if (!result.IsSuccessful) { return(BadRequest(result.Message)); } return(Ok()); }
public async Task CreateTicket(CreateTicketBindingModel model) { var newTicket = new Ticket(); //Here i give default values on the Priority and Agent because these are props only accessed by Admin string username = "******"; var agent = await FindUserIdByUserName(username); newTicket.AgentId = agent.Id; newTicket.Title = model.Title; newTicket.Priority = model.Priority; newTicket.Descripton = model.Description; newTicket.ProjectId = model.ProjectId; newTicket.AuthorId = model.AuthorId; newTicket.SendOn = model.SendOn; await _dbContext.Tickets.AddAsync(newTicket); await _dbContext.SaveChangesAsync(); }