Exemple #1
0
        public async Task <IActionResult> New([FromBody] FlBooking flBooking)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (User.Identity.Name.Equals(flBooking.UserId.ToString()) || User.IsInRole("Admin"))
            {
                try
                {
                    var user = await _userDB.User.SingleOrDefaultAsync(s => s.UserId.Equals(flBooking.UserId));

                    var detail = _context.FlightDetail.Where(s => s.DetailId.Equals(flBooking.DetailId))
                                 .Include(s => s.Dest).ThenInclude(s => s.Flight);
                    _context.FlBooking.Add(flBooking);
                    await _context.SaveChangesAsync();

                    var    Return   = flBooking.ReturnDate.HasValue ? flBooking.ReturnDate : null;
                    var    names    = flBooking.TravellersNames.Split(',');
                    var    surnames = flBooking.TravellersSurnames.Split(',');
                    string split    = user.Name + " " + user.Surname + "<br/>";
                    int    index    = 0;
                    if (names.Any())
                    {
                        foreach (string n in names)
                        {
                            split += n + " " + surnames[index++] + "<br/>";
                        }
                    }

                    var str = Return != null ? "ReturnTrip date: " + Return + "<br/>PayType: " + flBooking.PayType : "<br/>PayType: " + flBooking.PayType;

                    EmailMessage message = new EmailMessage("Flight Booking", "Hi " + user.Name + ",<br/><br/>" +
                                                            "You have just booked for a flight using our a web services, the full details of the booking are: <br/>" +
                                                            detail.First().Dest.Flight.Locale + "<br/>" + detail.First().Dest.Dest + "<br/>" +
                                                            flBooking.FlightType + "<br/>" + "Booked date for departure: " + flBooking.BookDate +
                                                            "<br/>Departure time: " + detail.First().Departure.Split(' ')[1] + "<br/>" +
                                                            str +
                                                            "<br/>Number of travellers: " + flBooking.Travellers +
                                                            "<br/>Travellers names are:<br/>" + split + "<br/>Total: R" + flBooking.Total +
                                                            "<br/><br/>Kind Regards,<br/>Booking.com");

                    message.FromAddresses.Add(new EmailAddress("Booking.com", "*****@*****.**"));
                    message.ToAddresses.Add(new EmailAddress(user.Name, user.Email));
                    new Send(message, _emailConfiguration);
                    return(CreatedAtAction("GetFlBooking", new { id = flBooking.BookDate }, flBooking));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    return(BadRequest("Internal error"));
                }
            }

            return(Unauthorized());
        }
Exemple #2
0
        public async Task <IActionResult> PutFlBooking([FromRoute] int id, [FromBody] FlBooking flBooking)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != flBooking.BookingId)
            {
                return(BadRequest());
            }

            _context.Entry(flBooking).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException uex)
            {
                if (!FlBookingExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw uex;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return(NoContent());
        }