public IHttpActionResult UpdateProfile(Models.UpdateUserProfile profile) { try { if (!ModelState.IsValid) { return(BadRequest("Passed information isn't passed.")); } using (NewsDBEntities newsDB = new NewsDBEntities()) { var user = newsDB.Users.FirstOrDefault(u => u.UserName == Request.GetOwinContext().Authentication.User.Identity.GetUserName()); user.FirstName = profile.FirstName; user.LastName = profile.LastName; user.BirthDate = profile.BirthDate; // Save the changes to the database newsDB.SaveChanges(); } return(Ok()); } catch (Exception ex) { logger.Error(ex, "A user or admin attempted to change their profile and it threw an exception: " + ex.Message); return(BadRequest("Something went wrong while updating the profile information.")); } }
/** * @brief Get news for specific page */ public List <News> GetNewsByPage(int page = 1) { var context = new NewsDBEntities(); var newsOnPage = context.News.Where(x => x.Page == page).ToList(); if (newsOnPage.Count() == 0) { newsOnPage = LoadNews(page); } return(newsOnPage); }
/** * @brief Get last 'size' news */ public List <News> GetAllNews(int size = 50) { var context = new NewsDBEntities(); if (context.News.Count() == _pageInfo.TotalItems) { return(context.News.OrderBy(x => x.Page).Skip(Math.Max(0, context.News.Count() - size)).ToList()); } else { LoadNews(); return(GetAllNews(size)); } }
public IHttpActionResult RegisterAdmin(Account newAdmin) { try { if (!ModelState.IsValid) { return(BadRequest("Passed information isn't valid.")); } // Start registering the new admin var userStore = new UserStore <IdentityUser>(new UserDBContext()); var userManager = new UserManager <IdentityUser>(userStore); var user = new IdentityUser(newAdmin.UserName); // Before completely registering the user, make sure the username isn't taken if (userManager.Users.Any(u => u.UserName == newAdmin.UserName)) { return(BadRequest("Username is taken.")); } // Add user to UserDB userManager.Create(user, newAdmin.Password); // Let's make the user an admin now userManager.AddClaim(user.Id, new Claim(ClaimTypes.Role, "admin")); // Add user to NewsDB so we can link comments to the user using (NewsDBEntities newsDB = new NewsDBEntities()) { var u = new DAL.User() { UserName = newAdmin.UserName, Password = "", FirstName = newAdmin.FirstName, LastName = newAdmin.LastName, BirthDate = newAdmin.BirthDate, }; newsDB.Users.Add(u); newsDB.SaveChanges(); } logger.Info(newAdmin.UserName + " was created as a new admin."); return(Ok()); } catch (Exception ex) { logger.Error(ex, "Admin creation threw an exception: " + ex.Message); return(BadRequest("Something went wrong")); } }
/// <summary> /// this changes the stored password in the NewsDB database. The password is hashed. /// </summary> /// <param name="iUser">This passes the IdentityUser into the database.</param> private void updatePassword(IdentityUser iUser) { try { // Change the user's password in NewsDB database using (NewsDBEntities newsDB = new NewsDBEntities()) { var user = newsDB.Users.FirstOrDefault(u => u.UserName == iUser.UserName); user.Password = iUser.PasswordHash; newsDB.SaveChanges(); } } catch (Exception ex) { logger.Error(ex, "An exception was thrown inside of the updatePassword function: " + ex.Message); throw ex; } }
/** * @brief Delete news by index */ public HttpResponseMessage DeleteNewsByIndex(int idx) { News news = GetNewsByIndex(idx); if (news == null) { throw new System.ArgumentOutOfRangeException(); } else { using (var context = new NewsDBEntities()) { context.News.Remove(news); } var status = new HttpResponseMessage() { StatusCode = HttpStatusCode.NoContent }; return(status); } }
public IHttpActionResult Register(Account newUser) { if (!ModelState.IsValid) { return(BadRequest("Model isn't valid.")); } // Start registering the user var userStore = new UserStore <IdentityUser>(new UserDBContext()); var userManager = new UserManager <IdentityUser>(userStore); var user = new IdentityUser(newUser.UserName); // Before completely registering the user, make sure the username isn't taken if (userManager.Users.Any(u => u.UserName == newUser.UserName)) { return(BadRequest("Username is taken.")); } // Add user to UserDB userManager.Create(user, newUser.Password); userManager.AddClaim(user.Id, new Claim(ClaimTypes.Role, "user")); // Add user to NewsDB so we can link comments to the user using (NewsDBEntities newsDB = new NewsDBEntities()) { var u = new DAL.User() { UserName = newUser.UserName, Password = "", FirstName = newUser.FirstName, LastName = newUser.LastName, BirthDate = newUser.BirthDate, }; newsDB.Users.Add(u); newsDB.SaveChanges(); } logger.Info(newUser.UserName + " was created."); return(Ok()); }
/** * @brief Load news and save in database * @details if page = 0 => load all news * if page > 0 => load news for one page */ private List <News> LoadNews(int page = 0) { var context = new NewsDBEntities(); var storedPages = context.News.Select(x => x.Page).Distinct().ToHashSet(); var news = new List <News>(); int start = page == 0 ? 1 : page; int end = page == 0 ? _pageInfo.TotalPages : page; for (var i = start; i <= end; ++i) { if (!storedPages.Contains(i)) { var newsBunch = GetDataFromNewsApi(GetUrl(i)); newsBunch.ForEach(x => x.Page = i); news.AddRange(newsBunch); } } context.News.AddRange(news); context.SaveChanges(); return(news); }
public GenericRepository() { _context = new NewsDBEntities(); _entities = _context.Set <T>(); }