public ActionResult DeleteAccount(int id) { try { if (id != 0) { PortfolioContentController controller = new PortfolioContentController(_context); if (controller.DeletePortfolio(id)) { // Searching right user with ID var user = _context.Users.Find(id); _context.Remove(user); _context.SaveChanges(); } } return(Ok("Account and all content has deleted succesfully!")); } catch (Exception ex) { return(BadRequest("Problem detected while deleting user. Error message: " + ex.InnerException)); } finally { _context.Dispose(); } }
public ActionResult DeleteSkill(int id) { try { // Removes all projects of the skill Projects project = null; do { project = (from p in _context.Projects where p.SkillId == id select p).FirstOrDefault(); if (project != null) { _context.Remove(project); _context.SaveChanges(); } } while (project != null); // Removes the skill var skill = _context.Skills.Find(id); if (skill != null) { _context.Remove(skill); _context.SaveChanges(); } return(Ok("Skill deleted succesfully!")); } catch (Exception ex) { return(BadRequest("Problem detected while deleting skill. Error message: " + ex.Message)); } finally { _context.Dispose(); } }
public ActionResult DeleteLink(int id) { try { var link = _context.SocialMediaLinks.Find(id); if (link != null) { _context.Remove(link); _context.SaveChanges(); } return(Ok("Link deleted succesfully!")); } catch (Exception ex) { return(BadRequest("Problem detected while deleting the link. Error message: " + ex.Message)); } finally { _context.Dispose(); } }
// Delete all message public bool DeleteAllImages(int id) { try { // Searching right message with ID var image = _context.ImageUrls.Find(id); // Deletion from the database is performed if (image != null) { _context.Remove(image); _context.SaveChanges(); } return(true); } catch { return(false); } }
public ActionResult DeleteMessage(int id) { try { // Searching right message with ID var message = _context.QuestbookMessages.Find(id); // Searching a visitor who wrote the message int visitorId = (from v in _context.Visitors where v.VisitorId == message.VisitorId select v.VisitorId).FirstOrDefault(); var visitor = _context.Visitors.Find(visitorId); // The count of messages for visitor int messageCount = (from q in _context.QuestbookMessages where q.VisitorId == visitorId select q).ToArray().Length; // Deletion from the database is performed if (message != null && visitor != null) { // If the visitor has multiple messages, only the message will be deleted if (messageCount > 1) { _context.Remove(message); } else { _context.Remove(message); _context.Remove(visitor); } _context.SaveChanges(); } return(Ok("Message deleted succesfully!")); } catch (Exception ex) { return(BadRequest("Problem detected while deleting user. Error message: " + ex.InnerException)); } finally { _context.Dispose(); } }
public ActionResult DeleteProject(int id) { try { var project = _context.Projects.Find(id); if (project != null) { _context.Remove(project); _context.SaveChanges(); } return(Ok("Project deleted succesfully!")); } catch (Exception ex) { return(BadRequest("Problem detected while deleting the project. Error message: " + ex.Message)); } finally { _context.Dispose(); } }
// Delete users portfolio and all content public bool DeletePortfolio(int id) { try { if (id != 0) { bool mailBool = false; bool messageBool = false; bool imageBool = false; bool linkBool = false; bool skillBool = false; // Searching right emails with user ID var emailIdArray = (from e in _context.Emails where e.UserId == id select e.EmailId).ToArray(); int emailArrayCount = emailIdArray.Count(); if (emailArrayCount > 0) { for (int i = 0; i < emailIdArray.Length; i++) { Emails email = _context.Emails.Find(emailIdArray[i]); _context.Remove(email); _context.SaveChanges(); } mailBool = true; } else { mailBool = true; } // Searching right questbook messages with user ID var messageIdArray = (from qm in _context.QuestbookMessages where qm.UserId == id select qm.MessageId).ToArray(); int messageArrayCount = messageIdArray.Count(); if (messageArrayCount > 0) { for (int i = 0; i < messageArrayCount;) { int messageId = messageIdArray[i]; QuestbookController controller = new QuestbookController(_context); messageBool = controller.DeleteAllMessages(messageId); if (messageBool) { i++; } } } else { messageBool = true; } // Searching images with user ID var imageIdArray = (from iu in _context.ImageUrls where iu.UserId == id select iu.UrlId).ToArray(); int urlArrayCount = imageIdArray.Count(); if (urlArrayCount > 0) { for (int i = 0; i < urlArrayCount;) { int urlId = imageIdArray[i]; ImagesController controller = new ImagesController(_context); imageBool = controller.DeleteAllImages(urlId); if (imageBool) { i++; } } } else { imageBool = true; } // Searching social media links with user ID var linkIdArray = (from sml in _context.SocialMediaLinks where sml.UserId == id select sml.LinkId).ToArray(); int linkArrayCount = linkIdArray.Count(); if (linkArrayCount > 0) { for (int i = 0; i < linkArrayCount;) { int linkId = linkIdArray[i]; SocialMediaController controller = new SocialMediaController(_context); linkBool = controller.DeleteAllLinks(linkId); if (linkBool) { i++; } } } else { linkBool = true; } // Searching social media links with portfolio ID var skillIdArray = (from s in _context.Skills where s.UserId == id select s.SkillId).ToArray(); int skillArrayCount = skillIdArray.Count(); if (skillArrayCount > 0) { for (int i = 0; i < skillArrayCount;) { int skillId = skillIdArray[i]; SkillsController controller = new SkillsController(_context); skillBool = controller.DeleteAllSkillAndProjects(skillId); if (skillBool) { i++; } } } else { skillBool = true; } // Searching right portfolio with user ID int portfolioId = (from pc in _context.PortfolioContent where pc.UserId == id select pc.PortfolioId).FirstOrDefault(); // At the end, search the right portfolio and remove it if ((mailBool = true) && (messageBool = true) && (imageBool = true) && (linkBool = true) && (skillBool = true)) { PortfolioContent portfolio = _context.PortfolioContent.Find(portfolioId); _context.Remove(portfolio); _context.SaveChanges(); } else { return(false); } } return(true); } catch { return(false); } }