예제 #1
0
        public IHttpActionResult UpdateLocation(LocationViewModel location)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            /*            var locationInDb = _context.Locations.SingleOrDefault(m => m.LocationId == id);*/
            using (var _context = new FoodiesEntities())
            {
                var locationInDb = _context.Locations.Where(m => m.LocationId == location.LocationId).FirstOrDefault <Location>();

                if (locationInDb == null)
                {
                    return(NotFound());
                }
                else
                {
                    locationInDb.LocationId = location.LocationId;
                    locationInDb.District   = location.District;
                    locationInDb.City       = location.City;
                    locationInDb.State      = location.State;
                    locationInDb.Country    = location.Country;

                    _context.SaveChanges();
                }
            }
            return(Ok(location));
        }
예제 #2
0
        public IHttpActionResult GetLocation(int id, bool includeLocation = true)
        {
            /*            var location = _context.Locations.SingleOrDefault(m => m.LocationId == id);*/

            LocationViewModel location = null;

            using (var _context = new FoodiesEntities())
            {
                location = _context.Locations.Where(s => s.LocationId == id)
                           .Select(s => new LocationViewModel()
                {
                    LocationId = s.LocationId,
                    District   = s.District,
                    City       = s.City,
                    State      = s.State,
                    Country    = s.Country
                }).SingleOrDefault <LocationViewModel>();
            }
            if (location == null)
            {
                return(NotFound());
            }

            return(Ok(location));
        }
예제 #3
0
        public IHttpActionResult GetStore(int id, bool includeLocation = true)
        {
            /*            var store = _context.Stores.SingleOrDefault(m => m.StoreId == id);*/

            StoreViewModel store = null;

            using (var _context = new FoodiesEntities())
            {
                store = _context.Stores.Include("Location")
                        .Where(s => s.StoreId == id)
                        .Select(s => new StoreViewModel()
                {
                    StoreId   = s.StoreId,
                    StoreName = s.StoreName,
                    Revenue   = s.Revenue,
                    Location  = s.Location == null || includeLocation == false ? null : new LocationViewModel
                    {
                        LocationId = s.Location.LocationId,
                        District   = s.Location.District,
                        City       = s.Location.City,
                        State      = s.Location.State,
                        Country    = s.Location.Country
                    }
                }).SingleOrDefault <StoreViewModel>();
            }
            if (store == null)
            {
                return(NotFound());
            }

            return(Ok(store));
        }
예제 #4
0
        //GET api/locations/
        public IHttpActionResult GetLocations()
        {
            IList <LocationViewModel> locations = null;

            using (var _context = new FoodiesEntities())
            {
                locations = _context.Locations.Select(s => new LocationViewModel()
                {
                    LocationId = s.LocationId,
                    District   = s.District,
                    City       = s.City,
                    State      = s.State,
                    Country    = s.Country
                }).ToList <LocationViewModel>();
            }

            /*var locations = _context.Location.Include(m => m.LocationId).ToList();*/

            /*if (locations.Count == 0)
             * {
             *  return NotFound();
             * }
             */

            return(Ok(locations));
        }
예제 #5
0
        //GET api/stores/
        public IHttpActionResult GetStores()
        {
            IList <StoreViewModel> stores = null;

            using (var _context = new FoodiesEntities())
            {
                stores = _context.Stores.Include("Location")
                         .Select(s => new StoreViewModel()
                {
                    StoreId   = s.StoreId,
                    StoreName = s.StoreName,
                    Revenue   = s.Revenue,
                    Location  = s.Location == null ? null: new LocationViewModel
                    {
                        LocationId = s.Location.LocationId,
                        District   = s.Location.District,
                        City       = s.Location.City,
                        State      = s.Location.State,
                        Country    = s.Location.Country
                    }
                }).ToList <StoreViewModel>();
            }

            /*var stores = _context.Store.Include(m => m.LocationId).ToList();*/

            /*if (stores.Count == 0)
             * {
             *  return NotFound();
             * }
             */

            return(Ok(stores));
        }
예제 #6
0
        public IHttpActionResult UpdateStore(StoreViewModel store)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            /*            var storeInDb = _context.Stores.SingleOrDefault(m => m.StoreId == id);*/
            using (var _context = new FoodiesEntities())
            {
                var storeInDb = _context.Stores.Where(m => m.StoreId == store.StoreId).FirstOrDefault <Store>();

                if (storeInDb == null)
                {
                    return(NotFound());
                }
                else
                {
                    storeInDb.StoreName  = store.StoreName;
                    storeInDb.LocationId = store.Location.LocationId;
                    storeInDb.Revenue    = store.Revenue;

                    _context.SaveChanges();
                }
            }
            return(Ok(store));
        }
예제 #7
0
        public IHttpActionResult Create(CustomerViewModel customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            using (var _context = new FoodiesEntities())
            {
                _context.Customers.Add(new Customer()
                {
                    CustomerId   = customer.CustomerId,
                    CustomerName = customer.CustomerName,
                    Purchase     = customer.Purchase,
                    StoreId      = customer.Store.StoreId
                });
                _context.SaveChanges();
            }

            return(Created("custoemrs/" + customer.CustomerId.ToString(), customer));
        }
예제 #8
0
        public IHttpActionResult CreateStore(StoreViewModel store)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            /*_context.Stores.Add(store);*/
            using (var _context = new FoodiesEntities())
            {
                _context.Stores.Add(new Store()
                {
                    StoreId    = store.StoreId,
                    StoreName  = store.StoreName,
                    Revenue    = store.Revenue,
                    LocationId = store.Location.LocationId
                });

                _context.SaveChanges();
            }
            return(Created("stores/" + store.StoreId, store));
        }
예제 #9
0
        public IHttpActionResult CreateLocation(LocationViewModel location)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            /*_context.Locations.Add(location);*/
            using (var _context = new FoodiesEntities())
            {
                _context.Locations.Add(new Location()
                {
                    LocationId = location.LocationId,
                    District   = location.District,
                    City       = location.City,
                    State      = location.State,
                    Country    = location.Country
                });

                _context.SaveChanges();
            }
            return(Created("locations/" + location.LocationId, location));
        }
예제 #10
0
        public IHttpActionResult DeleteLocation(int id)
        {
            if (id <= 0)
            {
                return(BadRequest());
            }
            using (var _context = new FoodiesEntities())
            {
                var locationInDb = _context.Locations.Where(m => m.LocationId == id).FirstOrDefault();

                if (locationInDb == null)
                {
                    return(BadRequest());
                }

                /*            _context.Locations.Remove(locationInDb);*/

                _context.Entry(locationInDb).State = EntityState.Deleted;
                _context.SaveChanges();
            }
            return(Ok());
        }
예제 #11
0
        public IHttpActionResult DeleteCustomer(int id)
        {
            if (id <= 0)
            {
                return(BadRequest());
            }

            Customer customerInDb = null;

            using (var _context = new FoodiesEntities())
            {
                customerInDb = _context.Customers.Where(s => s.CustomerId == id).FirstOrDefault();

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

                _context.Customers.Remove(customerInDb);
                _context.SaveChanges();
            }
            return(Ok(customerInDb));
        }
예제 #12
0
        //GET api/customers
        public IHttpActionResult GetCustomers()
        {
            IList <CustomerViewModel> customers = null;

            using (var _context = new FoodiesEntities())
            {
                customers = _context.Customers.Include("Store")
                            .Select(s => new CustomerViewModel()
                {
                    CustomerId   = s.CustomerId,
                    CustomerName = s.CustomerName,
                    Purchase     = s.Purchase,
                    Store        = s.StoreId == null? null : new StoreViewModel()
                    {
                        StoreId   = s.Store.StoreId,
                        StoreName = s.Store.StoreName,
                        Revenue   = s.Store.Revenue
                    }
                }
                                    ).ToList <CustomerViewModel>();
            };
            return(Ok(customers));
        }