public async Task <IActionResult> AddTicket(TicketToCreateDto ticketToCreate) { if (ticketToCreate.userId != null) { var userFromRepo = await _repo.GetUser(ticketToCreate.userId, false); if (userFromRepo == null) { return(BadRequest()); } if (userFromRepo.project.Id != ticketToCreate.projectId) { return(BadRequest()); } } var newTicket = _mapper.Map <Ticket>(ticketToCreate); newTicket.project = await _repo.GetProject(ticketToCreate.projectId); if (ticketToCreate.userId != null) { newTicket.User = await _repo.GetUser(ticketToCreate.userId, false); } if (ticketToCreate.CategoryId != 0) { newTicket.Status = await _repo.GetStatus(ticketToCreate.CategoryId); } if (ticketToCreate.StatusId != 0) { newTicket.Category = await _repo.GetCategory(ticketToCreate.CategoryId); } if (ticketToCreate.PriorityId != 0) { newTicket.Priority = await _repo.GetPriority(ticketToCreate.PriorityId); } _repo.Add(newTicket); if (await _repo.SaveAll()) { var ticketToReturn = _mapper.Map <TicketsForDetailed>(newTicket); return(Ok(ticketToReturn)); } throw new Exception("Ticket can't created"); }
public async Task <IActionResult> CreateTicketAsync([FromBody, SwaggerParameter(Description = "Values of the ticket to be created eg. which seat and what price", Required = true)] TicketToCreateDto ticketToCreateDto, [FromHeader(Name = "Accept"), SwaggerParameter(Description = "Media type to return eg. json or json+hateoas")] string mediaType, [FromRoute, SwaggerParameter(Description = "Id of the related movie", Required = true)] Guid movieId, [FromRoute, SwaggerParameter(Description = "Id of the related showtime", Required = true)] Guid showtimeId) { if (!await _showtimeRepository.MovieExists(movieId)) { return(NotFound()); } if (!await _ticketRepository.ShowtimeExists(showtimeId)) { return(NotFound()); } if (ticketToCreateDto.SeatId == Guid.Empty) { return(BadRequest()); //var availableSeat = await _seatRepository.GetFirstAvailableSeatByShowtimeIdAsync(showtimeId); //if (availableSeat == null) //{ // //Not sure if this is enough. Could be tested. // _logger.LogError("No available seats"); //} //ticketToCreateDto.SeatId = availableSeat.Id; } var ticket = Mapper.Map <Ticket>(ticketToCreateDto); ticket.ShowtimeId = showtimeId; ticket.Id = Guid.NewGuid(); _ticketRepository.AddTicket(ticket); if (!await _ticketRepository.SaveChangesAsync()) { _logger.LogError("Saving changes to database while creating a showtime failed"); } var ticketDto = Mapper.Map <TicketDto>(ticket); if (mediaType == "application/vnd.biob.json+hateoas") { var links = CreateLinksForTickets(ticketDto.Id, null); var linkedTicket = ticketDto.ShapeData(null) as IDictionary <string, object>; linkedTicket.Add("links", links); return(CreatedAtRoute("GetTicket", new { ticketId = ticketDto.Id }, linkedTicket)); } else { //Hmm why is dto used here when it's not in the other methods? return(CreatedAtRoute("GetTicket", new { ticketId = ticketDto.Id }, ticketDto)); } }