示例#1
0
        public async Task<IHttpActionResult> PostRoute(Route route)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            route.RouteId = Guid.NewGuid();
            db.Routes.Add(route);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException ex)
            {
                if (RouteExists(route.RouteId))
                {
                    return Conflict();
                }
                else
                {
                    throw ex;
                }
            }

            //generate seat details
            List<SeatDTO> seats = await (from b in db.Seats
                               where b.BusId.Equals(route.BusId)

                               select new SeatDTO()
                               {
                                   SeatId = b.SeatId,
                                   BusId = b.BusId,
                                   Company = b._bus.Company,
                                   BrandDesc = string.Empty,
                                   BusTypeDesc = string.Empty,
                                   SeatNo = b.SeatNo,
                                   Bookable = b.Bookable,
                                   Space = b.Space,
                                   SpecialSeat = b.SpecialSeat,
                                   Status = b.Status,
                                   UpperLower = b.UpperLower,
                                   Row = b.Row,
                                   Col = b.Col
                               }).OrderBy(u => u.Row).ThenBy(u => u.Col).ToListAsync();

            foreach (SeatDTO seat in seats)
            {
                RouteSeat sd = new RouteSeat();
               // sd.BusId = seat.BusId;
                sd.Bookable = seat.Bookable;
                sd.Col = seat.Col;
                sd.RouteId = route.RouteId;
                sd.Row = seat.Row;
                sd.RouteSeatId = seat.SeatId;
                sd.SeatNo = seat.SeatNo;
                sd.Space = seat.Space;
                sd.SpecialSeat = seat.SpecialSeat;
                sd.State = (short)(seat.Bookable?OBTSEnum.SeatState.Available:OBTSEnum.SeatState.NotAvailable);
                db.RouteSeats.Add(sd);
            }

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException ex)
            {
                    throw ex;
                
            }

            return CreatedAtRoute("DefaultApi", new { id = route.RouteId }, route);
        }
示例#2
0
        public async Task<IHttpActionResult> PutRoute(Guid id, Route route)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != route.RouteId)
            {
                return BadRequest();
            }

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

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

            return StatusCode(HttpStatusCode.NoContent);
        }