/// <summary> /// Given a DTO it creates the topic in the database. Don't forget to /// insert your DTO into the list OR reload the topics. NOTE: This will /// not keep any Id associated with this topic that is handed in. /// Finally, it returns the Id value added. It is possible to have /// multiple, identical topics with this create functionality. /// </summary> /// <param name="topic">Topic to add to the database</param> /// <returns></returns> public static long CreateTopic(TopicDTO topic) { Topics topics = new Topics(); topics.Topic = topic.Topic; context.Add <Topics>(topics); context.SaveChanges(); return(topics.Id); }
/// <summary> /// Updates the text of the topic identified by its Id. /// </summary> /// <param name="topic"></param> /// <returns></returns> public static bool UpdateTopic(TopicDTO topic) { var topicToUpdate = context.Topics.Where(x => x.Id == topic.Id).FirstOrDefault(); if (topicToUpdate != null) { topicToUpdate.Topic = topic.Topic; context.SaveChanges(); return(true); } return(false); }
/// <summary> /// Returns true if DTO was found and deleted. Returns false if the DTO /// was not found OR if the topic has beliefs associated with it. /// </summary> /// <param name="topic">Topic to delete.</param> /// <returns>true -> success</returns> public static bool DeleteTopic(TopicDTO topic) { var topics = context.Topics.Include(a => a.Beliefs).Where(x => x.Id == topic.Id).FirstOrDefault(); if (topics == null) { return(false); } if (topics.Beliefs.Count() > 0) { return(false); } context.Remove(topics); context.SaveChanges(); return(true); }