public async Task<IHttpActionResult> UpdatePopularPlace(popularPlace city)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest();
                }
                city.updatedBy = User.Identity.GetUserId();
                city.updatedOn = DateTime.UtcNow;
                db.Entry(city).State = EntityState.Modified;

                try
                {
                    await db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
                return StatusCode(HttpStatusCode.NoContent);
            }
            return BadRequest("Not login");
        }
 public async Task<IHttpActionResult> OfficeBranch(CompanyOffice branch, string city, string popularPlace, string exectLocation,string SaveOrUpdate)
 {
     //var company = await db.Companies.FindAsync(companyId);
     if (city != null)
     {
         var citydb = db.Cities.FirstOrDefault(x => x.cityName.Equals(city, StringComparison.OrdinalIgnoreCase));
         if (citydb == null)
         {
             City cit = new City();
             cit.cityName = city;
             cit.addedBy = User.Identity.GetUserId();
             cit.addedBy = User.Identity.GetUserId();
             cit.addedOn = DateTime.UtcNow;
             db.Cities.Add(cit);
             await db.SaveChangesAsync();
             // loc.cityId = cit.Id;
             branch.cityId = cit.Id;
             if (popularPlace != null)
             {
                 popularPlace pop = new popularPlace();
                 pop.cityId = cit.Id;
                 pop.name = popularPlace;
                 pop.addedBy = User.Identity.GetUserId();
                 pop.addedOn = DateTime.UtcNow;
                 db.popularPlaces.Add(pop);
                 await db.SaveChangesAsync();
                 //  loc.popularPlaceId = pop.Id;
                 branch.popularPlaceId = pop.Id;
             }
         }
         else
         {
             // loc.cityId = citydb.Id;
             branch.cityId = citydb.Id;
             if (popularPlace != null)
             {
                 var ppp = db.popularPlaces.FirstOrDefault(x => x.City.cityName.Equals(city, StringComparison.OrdinalIgnoreCase) && x.name.Equals(popularPlace, StringComparison.OrdinalIgnoreCase));
                 if (ppp == null)
                 {
                     popularPlace pop = new popularPlace();
                     pop.cityId = citydb.Id;
                     pop.name = popularPlace;
                     pop.addedBy = User.Identity.GetUserId();
                     pop.addedOn = DateTime.UtcNow;
                     db.popularPlaces.Add(pop);
                     await db.SaveChangesAsync();
                     //   loc.popularPlaceId = pop.Id;
                     branch.popularPlaceId = pop.Id;
                 }
                 else
                 {
                     //   loc.popularPlaceId = ppp.Id;
                     branch.popularPlaceId = ppp.Id;
                 }
             }
         }
         branch.exectLocation = exectLocation;
         if (SaveOrUpdate == "Save")
         {
             db.CompanyOffices.Add(branch);
         }
         else if (SaveOrUpdate == "Update")
         {
             //var bra = db.CompanyOffices.Find(branch.Id);
             //bra.popularPlaceId = branch.popularPlaceId;
             //bra.since = branch.since;
             //bra.cityId = branch.cityId;
             //bra.contactNo1 = branch.contactNo1;
             //bra.contactNo2 = branch.contactNo2;
             //await db.SaveChangesAsync();
             db.Entry(branch).State = EntityState.Modified;
         }
         try
         {
             await db.SaveChangesAsync();
         }
         catch (Exception e)
         {
             string s = e.ToString();
         }
     }
     var ret = await (from br in db.CompanyOffices
               where br.Id.Equals(branch.Id)
               select new
               {
                   id = br.Id,
                   cityId = br.cityId,
                   cityName = br.City.cityName,
                   popularPlace = br.popularPlace.name,
                   popularPlaceId = br.popularPlaceId,
                   contactNo1 = br.contactNo1,
                   contactNo2 = br.contactNo2,
                   since = br.since,
                   exectLocation = br.exectLocation,
               }).FirstOrDefaultAsync();
     return Ok(ret);
 }
 public void MyAdLocation(string city, string popularPlace, string exectLocation,ref Ad ad,string SaveOrUpdate)
 {
     AdsLocation loc = new AdsLocation();
     if (city != null)
     {
         var citydb = db.Cities.FirstOrDefault(x => x.cityName.Equals(city, StringComparison.OrdinalIgnoreCase));
         if (citydb == null)
         {
             City cit = new City();
             cit.cityName = city;
             cit.addedBy = System.Web.HttpContext.Current.User.Identity.GetUserId();
             cit.addedBy = System.Web.HttpContext.Current.User.Identity.GetUserId();
             cit.addedOn = DateTime.UtcNow;
             db.Cities.Add(cit);
             db.SaveChanges();
             loc.cityId = cit.Id;
             if (popularPlace != null)
             {
                 popularPlace pop = new popularPlace();
                 pop.cityId = cit.Id;
                 pop.name = popularPlace;
                 pop.addedBy = System.Web.HttpContext.Current.User.Identity.GetUserId();
                 pop.addedOn = DateTime.UtcNow;
                 db.popularPlaces.Add(pop);
                 db.SaveChanges();
                 loc.popularPlaceId = pop.Id;
             }
         }
         else
         {
             loc.cityId = citydb.Id;
             if (popularPlace != null)
             {
                 var ppp = db.popularPlaces.FirstOrDefault(x => x.City.cityName.Equals(city, StringComparison.OrdinalIgnoreCase) && x.name.Equals(popularPlace, StringComparison.OrdinalIgnoreCase));
                 if (ppp == null)
                 {
                     popularPlace pop = new popularPlace();
                     pop.cityId = citydb.Id;
                     pop.name = popularPlace;
                     pop.addedBy = System.Web.HttpContext.Current.User.Identity.GetUserId();
                     pop.addedOn = DateTime.UtcNow;
                     db.popularPlaces.Add(pop);
                     db.SaveChanges();
                     loc.popularPlaceId = pop.Id;
                 }
                 else
                 {
                     loc.popularPlaceId = ppp.Id;
                 }
             }
         }
         loc.exectLocation = exectLocation;
         loc.Id = ad.Id;
         if (SaveOrUpdate == "Save")
         {
             ad.AdsLocation = loc;
            // db.AdsLocations.Add(loc);
         }
         else if (SaveOrUpdate == "Update")
         {
             db.Entry(loc).State = EntityState.Modified;
         }
             db.SaveChanges();
         
     }
 }
 public async Task<IHttpActionResult> HeadOfficeLocation(Company branch, string city, string popularPlace, string exectLocation)
 {
    // var branch = await db.Companies.FindAsync(companyId);
     if (city != null)
     {
         var citydb = db.Cities.FirstOrDefault(x => x.cityName.Equals(city, StringComparison.OrdinalIgnoreCase));
         if (citydb == null)
         {
             City cit = new City();
             cit.cityName = city;
             cit.addedBy = User.Identity.GetUserId();
             cit.addedBy = User.Identity.GetUserId();
             cit.addedOn = DateTime.UtcNow;
             db.Cities.Add(cit);
             await db.SaveChangesAsync();
             // loc.cityId = cit.Id;
             branch.cityId = cit.Id;
             if (popularPlace != null)
             {
                 popularPlace pop = new popularPlace();
                 pop.cityId = cit.Id;
                 pop.name = popularPlace;
                 pop.addedBy = User.Identity.GetUserId();
                 pop.addedOn = DateTime.UtcNow;
                 db.popularPlaces.Add(pop);
                 await db.SaveChangesAsync();
                 //  loc.popularPlaceId = pop.Id;
                 branch.popularPlaceId = pop.Id;
             }
         }
         else
         {
             // loc.cityId = citydb.Id;
             branch.cityId = citydb.Id;
             if (popularPlace != null)
             {
                 var ppp = db.popularPlaces.FirstOrDefault(x => x.City.cityName.Equals(city, StringComparison.OrdinalIgnoreCase) && x.name.Equals(popularPlace, StringComparison.OrdinalIgnoreCase));
                 if (ppp == null)
                 {
                     popularPlace pop = new popularPlace();
                     pop.cityId = citydb.Id;
                     pop.name = popularPlace;
                     pop.addedBy = User.Identity.GetUserId();
                     pop.addedOn = DateTime.UtcNow;
                     db.popularPlaces.Add(pop);
                     await db.SaveChangesAsync();
                     //   loc.popularPlaceId = pop.Id;
                     branch.popularPlaceId = pop.Id;
                 }
                 else
                 {
                     //   loc.popularPlaceId = ppp.Id;
                     branch.popularPlaceId = ppp.Id;
                 }
             }
         }
         branch.exectLocation = exectLocation;
         
         try
         {
             db.Entry(branch).State = EntityState.Modified;
             await db.SaveChangesAsync();
         }
         catch (Exception e)
         {
             string s = e.ToString();
         }
     }
     //var ret = await (from br in db.CompanyOffices
     //                 where br.Id.Equals(branch.Id)
     //                 select new
     //                 {
     //                     id = br.Id,
     //                     cityId = br.cityId,
     //                     cityName = br.City.cityName,
     //                     popularPlace = br.popularPlace.name,
     //                     popularPlaceId = br.popularPlaceId,
     //                     contactNo1 = br.contactNo1,
     //                     contactNo2 = br.contactNo2,
     //                     since = br.since,
     //                     exectLocation = br.exectLocation,
     //                 }).FirstOrDefaultAsync();
     return Ok("Done");
 }