public void Post([FromBody] ParkActivity parkActivity) { _db.ParkActivities.Add(new ParkActivity { ParkId = parkActivity.ParkId, ActivityId = parkActivity.ActivityId }); _db.SaveChanges(); }
public static void getParks(ApplicationDbContext context) { if (context.Parks.Any()) { return; } string uri = BASE_URL + "/parks?limit=100"; string responsebody = ""; httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Add("X-Api-Key", API_KEY); httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); httpClient.BaseAddress = new Uri(uri); try { HttpResponseMessage response = httpClient.GetAsync(uri).GetAwaiter().GetResult(); if (response.IsSuccessStatusCode) { responsebody = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); } if (!responsebody.Equals("")) { JObject parsedResponse = JObject.Parse(responsebody); JArray parks = (JArray)parsedResponse["data"]; foreach (JObject jsonpark in parks) { Park p = new Park { ID = (string)jsonpark["id"], url = (string)jsonpark["url"], fullName = (string)jsonpark["fullName"], parkCode = (string)jsonpark["parkCode"], description = (string)jsonpark["description"], }; context.Parks.Add(p); string[] states = ((string)jsonpark["states"]).Split(","); foreach (string s in states) { State st = context.States.Where(c => c.ID == s).FirstOrDefault(); if (st != null) { StatePark sp = new StatePark() { state = st, park = p }; context.StateParks.Add(sp); context.SaveChanges(); } } JArray activities = (JArray)jsonpark["activities"]; if (activities.Count != 0) { foreach (JObject jsonactivity in activities) { Activity a = context.Activities.Where(c => c.ID == (string)jsonactivity["id"]).FirstOrDefault(); if (a == null) { a = new Activity { ID = (string)jsonactivity["id"], name = (string)jsonactivity["name"] }; context.Activities.Add(a); context.SaveChanges(); } ParkActivity pa = new ParkActivity { activity = a, park = p }; context.ParkActivities.Add(pa); } } JArray topics = (JArray)jsonpark["topics"]; if (topics.Count != 0) { foreach (JObject jsontopic in topics) { Topic t = context.Topics.Where(c => c.ID == (string)jsontopic["id"]).FirstOrDefault(); if (t == null) { t = new Topic { ID = (string)jsontopic["id"], name = (string)jsontopic["name"] }; context.Topics.Add(t); context.SaveChanges(); } ParkTopic pt = new ParkTopic { topic = t, park = p }; context.ParkTopics.Add(pt); } context.SaveChanges(); } } } } catch (Exception e) { Console.WriteLine(e.Message); } }
public async Task <IActionResult> Edit(string id, [Bind("url,fullName,parkCode,description,statenames,activitynames,topicnames")] CreatePark modifiedp) { try { if (ModelState.IsValid) { // Fetch the park that needs to be updated Park ptobeupdated = _context.Parks .Include(p => p.activities) .Include(p => p.topics) .Include(p => p.states) .Where(p => p.ID == id) .FirstOrDefault(); ptobeupdated.url = modifiedp.url; ptobeupdated.fullName = modifiedp.fullName; ptobeupdated.parkCode = modifiedp.parkCode; ptobeupdated.description = modifiedp.description; ptobeupdated.activities.Clear(); foreach (string aname in modifiedp.activitynames) { Activity a = _context.Activities.Where(a => a.name == aname).FirstOrDefault(); ParkActivity pa = new ParkActivity() { park = ptobeupdated, activity = a }; ptobeupdated.activities.Add(pa); } ptobeupdated.topics.Clear(); foreach (string tname in modifiedp.topicnames) { Topic t = _context.Topics.Where(t => t.name == tname).FirstOrDefault(); ParkTopic pt = new ParkTopic() { park = ptobeupdated, topic = t }; ptobeupdated.topics.Add(pt); } ptobeupdated.states.Clear(); foreach (string sname in modifiedp.statenames) { State s = _context.States.Where(s => s.ID == sname).FirstOrDefault(); StatePark sp = new StatePark() { park = ptobeupdated, state = s }; ptobeupdated.states.Add(sp); } _context.Update(ptobeupdated); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Thanks), new { message = "Thanks! The record has been edited." })); } } catch (DbUpdateException /* ex */) { // Log the error (uncomment ex variable name and write a log. ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } Dictionary <string, string> dict = new Dictionary <string, string>(); foreach (State i in _context.States) { dict.Add(i.ID, i.name); } List <string> anames = await _context.Activities.Select(p => p.name).ToListAsync(); List <string> tnames = await _context.Topics.Select(p => p.name).ToListAsync(); ViewBag.statedict = dict; ViewBag.anames = anames; ViewBag.tnames = tnames; return(View(modifiedp)); }