public ActionResult AddOrUpdateLinks(int id, [FromBody] JsonElement jsonElement) { // Converts json element to string string json = System.Text.Json.JsonSerializer.Serialize(jsonElement); // Converts json string to JSON object var obj = JsonConvert.DeserializeObject <JObject>(json); try { // Converts nested "Services" JSON object to an array and save count of an array to variable var servicesArray = obj["Services"].ToArray(); int servicesArrayCount = servicesArray.Count(); // Adds as many links as the count of an array indicates for (int a = 0; a < servicesArrayCount; a++) { int linkId = int.Parse(servicesArray[a]["LinkId"].ToString()); // If the link is new (linkId == 0), a create operation is made to database. Otherwise an update if (linkId == 0) { SocialMediaLinks link = new SocialMediaLinks { UserId = id, ServiceId = int.Parse(servicesArray[a]["ServiceId"].ToString()), Link = servicesArray[a]["Link"].ToString() }; _context.SocialMediaLinks.Add(link); _context.SaveChanges(); } else { // Updates the link SocialMediaLinks link = new SocialMediaLinks { UserId = id, ServiceId = int.Parse(servicesArray[a]["ServiceId"].ToString()), Link = servicesArray[a]["Link"].ToString() }; if (!UpdateLink(linkId, link)) { return(BadRequest("Problem detected while updating social media link. Link ID: " + linkId)); } } } return(Ok("New links has added to portfolio!")); } catch (Exception ex) { return(BadRequest("Problem detected while adding a new link. Error message: " + ex.Message)); } finally { _context.Dispose(); } }
public void SortData() { if (SocialMediaLinks.Any() && User != null) { //links which the user does not already have var nonUserSocialMediaLinks = SocialMediaLinks .Where(s => User.UserSocialMediaLinks.All(l => l.SocialMediaLinkId != s.SocialMediaLinkId)).ToList(); var mappedNonUserLinks = nonUserSocialMediaLinks .Select(x => new SocialMediaLinkViewModel { SocialMediaLinkId = x.SocialMediaLinkId, Name = x.Name }).ToList(); //links which the user already has var userSocialMediaLinks = SocialMediaLinks .Where(s => User.UserSocialMediaLinks.Any(l => l.SocialMediaLinkId == s.SocialMediaLinkId)).ToList(); var mappedUserLinks = userSocialMediaLinks.Join(User.UserSocialMediaLinks, ul => ul.SocialMediaLinkId, l => l.SocialMediaLinkId, (ul, l) => new SocialMediaLinkViewModel { SocialMediaLinkId = ul.SocialMediaLinkId, Name = ul.Name, Link = l.Link } ).ToList(); SocialMediaLinkItems.AddRange(mappedUserLinks); SocialMediaLinkItems.AddRange(mappedNonUserLinks); } if (User != null && User.Groups.Any()) { UsersGroups = User.Groups.Select( g => new CheckboxItemViewModel { Id = g.GroupId, Name = g.Name, IsSelected = true }).ToList(); } OtherGroups = Groups.Where(g => User.Groups.All(ug => ug.GroupId != g.GroupId)) .Select(g => new CheckboxItemViewModel { Id = g.GroupId, Name = g.Name, IsSelected = false }).ToList(); }
public ActionResult Update(SocialMediaLinks socialmedia) { if (ModelState.IsValid) { db = new StorehouseDBContext(); db.Entry(socialmedia).State = EntityState.Modified; db.SaveChanges(); } return(RedirectToAction("Index", "Sliders")); }
public ActionResult Add(SocialMediaLinks socialmedia) { if (ModelState.IsValid) { db = new StorehouseDBContext(); socialmedia.Id = Guid.NewGuid(); db.SocialMediaLinks.Add(socialmedia); db.SaveChanges(); } return(RedirectToAction("Index", "SocialLinks")); }
public IHttpActionResult PostSocialMediaLink(SocialMediaLinks SocialMediaLink) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _repo.UpsertSocialMediaLink(SocialMediaLink); return(CreatedAtRoute("DefaultApi", new { id = SocialMediaLink.Id }, SocialMediaLink)); }
public IHttpActionResult GetSocialMediaLink(string id) { SocialMediaLinks SocialMediaLink = _repo.GetSocialMediaLink(id); if (SocialMediaLink == null) { return(NotFound()); } return(Ok(SocialMediaLink)); }
public IHttpActionResult PutSocialMediaLink(string id, SocialMediaLinks SocialMediaLink) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (new Guid(id) != SocialMediaLink.Id) { return(BadRequest()); } _repo.UpsertSocialMediaLink(SocialMediaLink); return(StatusCode(HttpStatusCode.NoContent)); }
// Update a single link for social media service public bool UpdateLink(int id, SocialMediaLinks newLink) { try { // Get a link that need to update. New data is placed to database SocialMediaLinks oldLink = (from sml in _context.SocialMediaLinks where sml.LinkId == id select sml).FirstOrDefault(); oldLink.ServiceId = newLink.ServiceId; oldLink.Link = newLink.Link; _context.SaveChanges(); return(true); } catch { return(false); } }
public void UpsertSocialMediaLink(SocialMediaLinks input) { try { if (input.Id == null) { input.Id = Guid.NewGuid(); db.SocialMediaLinks.Add(input); db.SaveChanges(); } else { db.Entry(input).State = EntityState.Modified; db.SaveChanges(); } } catch (Exception ex) { throw ex; } }