Пример #1
0
        public async Task<IHttpActionResult> PutBookingDetail(Guid id, BookingDetail bookingDetail)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != bookingDetail.BookingDetailId)
            {
                return BadRequest();
            }

            db.Entry(bookingDetail).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookingDetailExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Пример #2
0
        public async Task<IHttpActionResult> PostBookingDetail(BookingDetail bookingDetail)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.BookingDetails.Add(bookingDetail);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (BookingDetailExists(bookingDetail.BookingDetailId))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = bookingDetail.BookingDetailId }, bookingDetail);
        }
Пример #3
0
        public async Task<IHttpActionResult> InsertBooking(BookingDTO booking)
        {
            using (var db = new ApplicationDbContext())
            {


                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }


                Booking _booking = new Booking();
                if (booking != null)
                {

                    _booking.BookingId = Guid.NewGuid();
                    _booking.BookingOn = DateTime.Now;
                    _booking.BookingRefId = _booking.BookingRefId.GetRandomString(6); //Generate Booking Ref ID
                    _booking.BookingStatus = booking.BookingStatus;
                    _booking.ContactNo = booking.ContactNo;
                    _booking.Cupon = booking.Cupon;
                    _booking.Discount = booking.Discount;
                    _booking.Email = booking.Email;
                    _booking.MainContact = booking.MainContact;
                    _booking.UserId = booking.UserId;

                    db.Bookings.Add(_booking);
                }
                
                BookingDetail bd = new BookingDetail();
                if (booking.DepartBookingDetail != null) { 
                    bd = new BookingDetail();
                    bd.BookingDetailId = Guid.NewGuid();
                    bd.BookingId = _booking.BookingId;
                    bd.RouteFare = booking.DepartBookingDetail.RouteFare;
                    bd.RouteId = booking.DepartBookingDetail.RouteId;
                    db.BookingDetails.Add(bd);

                    foreach (BookingPassengerDTO p in booking.DepartBookingDetail.bookingPassengers)
                    {
                        BookingPassenger passenger = new BookingPassenger();
                        passenger.Age = p.Age;
                        passenger.BookingDetailId = bd.BookingDetailId;
                        passenger.BookingPassengerId = Guid.NewGuid();
                        passenger.Gender = p.Gender;
                        passenger.IDNumber = p.IDNumber;
                        passenger.IDType = p.IDType;
                        passenger.PassengerName = p.PassengerName;
                        passenger.RouteSeatId = p.RouteSeatId;

                        db.BookingPassengers.Add(passenger);
                    }
                }

                BookingDetail bd1 = new BookingDetail();
                if (booking.ReturnBookingDetail != null)
                {

                    bd1.BookingDetailId = Guid.NewGuid();
                    bd1.BookingId = booking.BookingId;
                    bd1.RouteFare = booking.ReturnBookingDetail.RouteFare;
                    bd1.RouteId = booking.ReturnBookingDetail.RouteId;
                    db.BookingDetails.Add(bd1);

                    foreach (BookingPassengerDTO p in booking.ReturnBookingDetail.bookingPassengers)
                    {
                        BookingPassenger passenger = new BookingPassenger();
                        passenger.Age = p.Age;
                        passenger.BookingDetailId = bd1.BookingDetailId;
                        passenger.BookingPassengerId = Guid.NewGuid();
                        passenger.Gender = p.Gender;
                        passenger.IDNumber = p.IDNumber;
                        passenger.IDType = p.IDType;
                        passenger.PassengerName = p.PassengerName;
                        passenger.RouteSeatId = p.RouteSeatId;

                        db.BookingPassengers.Add(passenger);
                    }
                }

        
                try
                {
                    await db.SaveChangesAsync();
                    
                    short seatstate = 0;
                    if (booking.BookingStatus == (short)OBTSEnum.BookState.Confirmed)
                        seatstate = (short)OBTSEnum.SeatState.Confirmed;
                    if (booking.BookingStatus == (short)OBTSEnum.BookState.UnConfirm)
                        seatstate = (short)OBTSEnum.SeatState.UnConfirm;
                    if (booking.BookingStatus == (short)OBTSEnum.BookState.Cancelled)
                        seatstate = (short)OBTSEnum.SeatState.Available;

                    //booking status to seat status =>booking confirmed,unconfirm,cancelled
                    foreach (BookingPassengerDTO p in booking.DepartBookingDetail.bookingPassengers)
                    {
                        using (var context = new ApplicationDbContext())
                        {
                            context.Database.ExecuteSqlCommand("UPDATE RouteSeats SET State={0}  WHERE RouteSeatId = {1}", seatstate, p.RouteSeatId);
                        }
                    }
                    if(booking.ReturnBookingDetail!=null)
                    {
                        foreach (BookingPassengerDTO p in booking.ReturnBookingDetail.bookingPassengers)
                        {
                            using (var context = new ApplicationDbContext())
                            {
                                context.Database.ExecuteSqlCommand("UPDATE RouteSeats SET State={0}  WHERE RouteSeatId = {1}", seatstate, p.RouteSeatId);
                            }
                        }
                    }
                   
                }

                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0}:{1}",
                                validationErrors.Entry.Entity.ToString(),
                                validationError.ErrorMessage);
                            // raise a new exception nesting
                            // the current instance as InnerException
                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }


                return Ok(booking);
            }
        }