Exemplo n.º 1
0
        public IHttpActionResult GetEstablishment(int id)
        {
            Establishment establishment = db.Establishments.Find(id);

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

            var temp = new tempEstablishment(establishment.Id, establishment.Name, establishment.Address, establishment.WorkingHours, establishment.MainRating, establishment.Description, establishment.PhoneNumber, establishment.Category.Name, establishment.Location.PostCode, establishment.Location.City, establishment.Owner.UserName);

            return(Ok(temp));
        }
Exemplo n.º 2
0
        public IHttpActionResult PutEstablishment(int id, tempEstablishment establishment)
        {
            Establishment newEstablishment = new Establishment();

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

            if (id != establishment.Id)
            {
                return(BadRequest());
            }
            else
            {
                var temp = from estbl in db.Establishments
                           where estbl.Id == id
                           select estbl;
                newEstablishment = temp.Single();

                var  locationList = db.Locations.ToList();
                bool locationFlag = true;

                Location newLocation = new Location()
                {
                    City = establishment.City, PostCode = establishment.PostalCode, Country = newEstablishment.Location.Country
                };

                foreach (var location in locationList)
                {
                    if (location.Country == newEstablishment.Location.Country && location.City == establishment.City && location.PostCode == establishment.PostalCode)
                    {
                        newLocation  = location;
                        locationFlag = false;
                    }
                }

                if (locationFlag)
                {
                    db.Locations.Add(newLocation);
                }

                newEstablishment.Location = newLocation;

                newEstablishment.Address = establishment.Address;

                var categoriesList = db.Categories.ToList();
                if (establishment.CategoryName == "Restaurant")
                {
                    newEstablishment.Category = categoriesList[0];
                }
                else if (establishment.CategoryName == "Fast Food")
                {
                    newEstablishment.Category = categoriesList[1];
                }
                else if (establishment.CategoryName == "Bar")
                {
                    newEstablishment.Category = categoriesList[2];
                }
                else if (establishment.CategoryName == "Coffee Shop")
                {
                    newEstablishment.Category = categoriesList[3];
                }

                newEstablishment.PhoneNumber  = establishment.PhoneNumber;
                newEstablishment.WorkingHours = establishment.WorkingHours;

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

                try
                {
                    db.SaveChanges();
                    return(Ok());
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EstablishmentExists(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }