Ejemplo n.º 1
0
        public async Task <TicketDetailsDTO> GetDetailsAsync(int id)
        {
            var ticket = await _unitOfWork.Set <Ticket>()
                         .DetailsAsync(id);

            if (ticket == null)
            {
                throw new NotFoundException("Ticket with such id was not found");
            }

            return(TicketDetailsDTO.Create(ticket));
        }
Ejemplo n.º 2
0
        public TicketDetailsDTO GetDetails(int id)
        {
            var ticket = _unitOfWork.Set <Ticket>()
                         .Details(x => x.Id == id).FirstOrDefault();

            if (ticket == null)
            {
                throw new NotFoundException("Ticket with such id was not found");
            }

            return(TicketDetailsDTO.Create(ticket));
        }
        public ActionResult <TicketDetailsDTO> GetTicketById(long id)
        {
            LoginToken <Customer> customer_token = DesirializeToken();
            Ticket ticket = null;

            try
            {
                ticket = _loggedInCustomerFacade.GetTicketById(customer_token, id);
                if (ticket == null)
                {
                    return(NotFound());
                }
            }
            catch (WrongCustomerException)
            {
                return(Forbid());
            }

            TicketDetailsDTO ticketDetailsDTO = _mapper.Map <TicketDetailsDTO>(ticket);

            return(Ok(ticketDetailsDTO));
        }
Ejemplo n.º 4
0
        public async Task Purchase_And_Get_New_Ticket()
        {
            await TestHelpers.Customer_Login(_httpClient);

            long flight_id = 1;

            var response = await _httpClient.PostAsync("api/tickets",
                                                       new StringContent(JsonSerializer.Serialize(flight_id), Encoding.UTF8, MediaTypeNames.Application.Json));

            var responseContent = await response.Content.ReadAsStringAsync();

            TicketDetailsDTO ticketPostResult = JsonSerializer.Deserialize <TicketDetailsDTO>(responseContent, new JsonSerializerOptions {
                PropertyNameCaseInsensitive = true
            });
            var    linkGeneratorService = _serviceProvider.GetRequiredService <LinkGenerator>();
            string createdPath          = linkGeneratorService.GetPathByAction(nameof(CustomerFacadeController.GetTicketById), "CustomerFacade", new { id = ticketPostResult.Id });

            Assert.AreEqual(response.Headers.Location.OriginalString, createdPath);

            Assert.AreEqual(ticketPostResult.Id, 1);

            Assert.AreEqual(ticketPostResult.Flight.Id, 1);

            Assert.AreEqual(ticketPostResult.Customer.Id, 1);

            var response2 = await _httpClient.GetAsync($"api/tickets/{ticketPostResult.Id}");

            Assert.AreEqual(HttpStatusCode.OK, response2.StatusCode);

            var responseContent2 = await response2.Content.ReadAsStringAsync();

            TicketDetailsDTO ticketGetResult = JsonSerializer.Deserialize <TicketDetailsDTO>(responseContent, new JsonSerializerOptions {
                PropertyNameCaseInsensitive = true
            });

            TestHelpers.CompareProps(ticketGetResult, ticketPostResult);
        }
        public ActionResult <TicketDetailsDTO> PurchaseTicket([FromBody] long flight_id)
        {
            LoginToken <Customer> customer_token = DesirializeToken();
            Ticket ticket = null;
            string uri    = null;

            Flight flight = _loggedInCustomerFacade.GetFlightById(flight_id);

            if (flight == null)
            {
                return(NotFound());
            }

            if (flight.RemainingTickets < 1 || flight.DepartureTime <= DateTime.Now.AddMinutes(15))
            {
                return(StatusCode(StatusCodes.Status410Gone));
            }

            try
            {
                ticket = _loggedInCustomerFacade.PurchaseTicket(customer_token, flight);

                uri = _linkGenerator.GetPathByAction(nameof(GetTicketById), "customerfacade", new { id = ticket.Id });
            }
            catch (RecordAlreadyExistsException)
            {
                return(Conflict());
            }
            catch (TicketPurchaseFailedException)
            {
                return(StatusCode(StatusCodes.Status410Gone));
            }

            TicketDetailsDTO ticketDetailsDTO = _mapper.Map <TicketDetailsDTO>(ticket);

            return(Created(uri, ticket));
        }
Ejemplo n.º 6
0
        public async Task Get_Ticket_Of_Another_Customer_Should_Return_Forbidden()
        {
            CreateCustomerDTO createCustomerDTO = new CreateCustomerDTO
            {
                FirstName        = "Customer2",
                LastName         = "Custom2",
                Address          = null,
                PhoneNumber      = "052-5555512",
                CreditCardNumber = null,
                User             = new CreateUserDTO
                {
                    UserName = "******",
                    Password = "******",
                    Email    = "*****@*****.**",
                }
            };

            await TestHelpers.Customer_Login(_httpClient, createCustomerDTO);

            long flight_id = 1;

            var response = await _httpClient.PostAsync("api/tickets",
                                                       new StringContent(JsonSerializer.Serialize(flight_id), Encoding.UTF8, MediaTypeNames.Application.Json));

            var responseContent = await response.Content.ReadAsStringAsync();

            TicketDetailsDTO ticketPostResult = JsonSerializer.Deserialize <TicketDetailsDTO>(responseContent, new JsonSerializerOptions {
                PropertyNameCaseInsensitive = true
            });

            await TestHelpers.Customer_Login(_httpClient);

            var response2 = await _httpClient.GetAsync($"api/tickets/{ticketPostResult.Id}");

            Assert.AreEqual(HttpStatusCode.Forbidden, response2.StatusCode);
        }