/// <summary> /// Adds a new Article to the database and returns the success of the operation. /// The id field of the parameter will be ignored when adding to the database. /// </summary> public static bool AddArticle(Dbo.Article article) { try { using (ProjectDBEntities ctx = new ProjectDBEntities()) { ctx.T_Article.Add(new T_Article() { Title = article.Title, IdAuthor = article.IdAuthor, Date = article.Date, Image = article.Image, Text = article.Text, Viewcount = 0 }); if (ctx.SaveChanges() == 0) { return(false); } } return(true); } catch (Exception) { return(false); } }
/// <summary> /// Returns a list of all Article in the database. /// </summary> public static List <Dbo.Article> GetArticleList() { List <Dbo.Article> res = new List <Dbo.Article>(); using (ProjectDBEntities ctx = new ProjectDBEntities()) { var primitiveList = ctx.T_Article.ToList(); foreach (var article in primitiveList) { res.Add(new Dbo.Article(article.Id, article.Title, article.IdAuthor, article.Date, article.Image, article.Text, article.Viewcount)); } } return(res); }
/// <summary> /// Returns a User with matching Name and Password from the database, or null if no matching user was found. /// The returned User will have an empty password field for security reasons. /// </summary> public static Dbo.User GetUserByCredentials(string name, string password) { try { using (ProjectDBEntities ctx = new ProjectDBEntities()) { var user = (from tmp in ctx.T_User where tmp.Name == name && tmp.Password == password select tmp).First(); return(new Dbo.User(user.Id, user.Name, "", user.Admin)); } } catch (Exception) { return(null); } }
/// <summary> /// Returns a User with matching ID from the database, or null if no matching user was found. /// The returned User will have an empty password field for security reasons. /// </summary> public static Dbo.User GetUserById(long id) { try { using (ProjectDBEntities ctx = new ProjectDBEntities()) { var user = (from tmp in ctx.T_User where tmp.Id == id select tmp).First(); return(new Dbo.User(user.Id, user.Name, "", user.Admin)); } } catch (Exception) { return(null); } }
/// <summary> /// Increments the viewcounter of the matching article and returns the success of the operation. /// </summary> public static bool IncrementArticleViewcountById(long id) { try { using (ProjectDBEntities ctx = new ProjectDBEntities()) { var article = (from tmp in ctx.T_Article where tmp.Id == id select tmp).First(); article.Viewcount = article.Viewcount + 1; if (ctx.SaveChanges() == 0) { return(false); } } return(true); } catch (Exception) { return(false); } }
/// <summary> /// Adds a new User to the database and returns the success of the operation. /// The id field of the parameter will be ignored when adding to the database. /// </summary> public static bool AddUser(Dbo.User user) { try { using (ProjectDBEntities ctx = new ProjectDBEntities()) { ctx.T_User.Add(new T_User() { Name = user.Name, Password = user.Password, Admin = user.Admin }); if (ctx.SaveChanges() == 0) { return(false); } } return(true); } catch (Exception) { return(false); } }