コード例 #1
0
        public async Task <HttpResponseMessage> Get(int id = -1)
        {
            if (id != -1)
            {
                Restaurant res = await _db.Restaurants.Where(b => b.Id == id && !b.Deleted).FirstOrDefaultAsync();

                if (res == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NoContent, "No Restaurant Found With ID"));
                }

                LibBookingService.Dtos.Restaurant restaurant = CreateRestaurantFromDbRestaurant(res);

                return(Request.CreateResponse(HttpStatusCode.OK, restaurant));
            }
            else
            {
                IEnumerable <Restaurant> res = await _db.Restaurants.Where(b => b.Deleted != true).ToListAsync();

                IEnumerable <LibBookingService.Dtos.Restaurant> restaurants = res.Select(b => CreateRestaurantFromDbRestaurant(b)).OrderBy(b => b.Name);

                return(restaurants.Any() ?
                       Request.CreateResponse(HttpStatusCode.OK, restaurants) :
                       Request.CreateErrorResponse(HttpStatusCode.NoContent, "No Restaurants"));
            }
        }
コード例 #2
0
        public async Task <HttpResponseMessage> Update(int id, LibBookingService.Dtos.Restaurant restaurant)
        {
            try
            {
                Restaurant r = await _db.Restaurants.Where(rr => rr.Id == id).FirstOrDefaultAsync();

                r.Company_id        = restaurant.CompanyId;
                r.Name              = restaurant.Name;
                r.PhoneNo           = restaurant.PhoneNo;
                r.AddressStreet     = restaurant.AddressStreet;
                r.AddressCounty     = restaurant.AddressCounty;
                r.AddressTown       = restaurant.AddressTown;
                r.AddressPostalCode = restaurant.AddressPostalCode;

                _db.SetModified(r);
                await _db.SaveChangesAsync();

                LibBookingService.Dtos.Restaurant res = CreateRestaurantFromDbRestaurant(r);

                return(Request.CreateResponse(HttpStatusCode.OK, res));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Failed"));
            }
        }
コード例 #3
0
        public async Task <HttpResponseMessage> Post(LibBookingService.Dtos.Restaurant restaurant)
        {
            try
            {
                Restaurant newRestaurant = _db.Restaurants.Add(new Restaurant
                {
                    Company_id        = restaurant.CompanyId,
                    Name              = restaurant.Name,
                    PhoneNo           = restaurant.PhoneNo,
                    AddressStreet     = restaurant.AddressStreet,
                    AddressCounty     = restaurant.AddressCounty,
                    AddressTown       = restaurant.AddressTown,
                    AddressPostalCode = restaurant.AddressPostalCode
                });
                await _db.SaveChangesAsync();

                if (restaurant.Tables != null)
                {
                    foreach (LibBookingService.Dtos.Table t in restaurant.Tables)
                    {
                        _db.Tables.Add(new Table
                        {
                            Restaurant_id   = newRestaurant.Id,
                            AdditionalNotes = t.AdditionalNotes,
                            NoSeats         = t.NoSeats,
                            Active          = t.Active
                        });
                        await _db.SaveChangesAsync();
                    }
                }

                if (restaurant.MenuItems != null)
                {
                    foreach (LibBookingService.Dtos.MenuItem m in restaurant.MenuItems)
                    {
                        _db.RestaurantMenuItems.Add(new RestaurantMenuItem
                        {
                            Restaurant_id = newRestaurant.Id,
                            MenuItem_id   = m.Id
                        });
                        await _db.SaveChangesAsync();
                    }
                }

                return(Request.CreateResponse(HttpStatusCode.OK, CreateRestaurantFromDbRestaurant(newRestaurant)));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Failed"));
            }
        }