/// <summary> /// Aktiverar en inaktiverad användare. /// </summary> /// <param name="adminId"></param> /// <param name="userId"></param> /// <returns>true om lyckad, annars false</returns> public static bool ActivateUser(int adminId, int userId) { try { if (Helper.CheckIfAdmin(db.Users.FirstOrDefault(a => a.Id == adminId))) { var user = db.Users.FirstOrDefault(u => u.Id == userId); if (user != null) { user.IsActive = true; db.SaveChanges(); if (Helper.DoesUserExist(db.Users.FirstOrDefault(u => u.Id == userId && u.IsActive))) { return(View.ActiveInactive("active", user.Name)); } } return(View.SomethingWentWrong()); } return(View.SomethingWentWrong()); } catch (Exception) { return(View.SomethingWentWrong()); } }
/// <summary> /// Lägg till ny bok. /// </summary> /// <param name="adminId"></param> /// <param name="title"></param> /// <param name="author"></param> /// <param name="price"></param> /// <param name="amount"></param> /// <returns>true om boken redan finns eller lades till korrekt, annars false.</returns> public static bool AddBook(int adminId, string title, string author, int price, int amount) { try { var book = db.Books.FirstOrDefault(b => b.Title == title); if (Helper.CheckIfAdmin(db.Users.FirstOrDefault(a => a.Id == adminId))) { if (book != null) { SetAmount(adminId, book.Id, amount); db.SaveChanges(); return(true); } else { db.Books.Add(new Book() { Title = title, Author = author, Price = price, Amount = amount }); db.SaveChanges(); if (Helper.DoesBookExist(db.Books.FirstOrDefault(b => b.Title == title))) { return(View.AddBook(title)); } else { return(View.SomethingWentWrong()); } } } return(View.SomethingWentWrong()); } catch (Exception) { return(View.SomethingWentWrong()); } }
/// <summary> /// Adds a book. Returns true if User is admin and input contains data. If book already exists /// it just adds onto that books amount. /// </summary> /// <param name="adminId"></param> /// <param name="title"></param> /// <param name="author"></param> /// <param name="price"></param> /// <param name="amount"></param> /// <returns>A bool true if sucess, else false</returns> public bool AddBook(int adminId, string title, string author, int price, int amount) { try { if (IsUserAdmin(adminId) && IsUserActive(adminId)) { if (BookExist(title, author)) { //Book does exist already var book = db.Books.Where(b => b.Title.Equals(title) && b.Author.Equals(author)).FirstOrDefault(); if (book != null) { book.Amount += amount; SetUserActive(adminId); db.SaveChanges(); return(true); } else { return(false); } } else { //Book does not exist if (title != string.Empty && author != string.Empty && price > 0 && amount > 0) { db.Books.Add(new Book { Title = title, Author = author, Price = price, Amount = amount }); SetUserActive(adminId); db.SaveChanges(); return(true); } else { return(false); } } } else { //User is not admin return(false); } } catch (Exception ex) { Debug.WriteLine("Failed to add book " + ex); return(false); } }
public static void Seed() { using (var db = new WebbShopContext()) { if (db.Users.Count() == 0) { db.Users.Add(new User() { Name = "Administrator", Password = "******", IsAdmin = true, IsActive = true, SoldBooks = new List <SoldBook>() }); db.Users.Add(new User() { Name = "TestClient", SoldBooks = new List <SoldBook>() }); db.SaveChanges(); } if (db.Categories.Count() == 0) { db.Categories.Add(new Category() { Name = "Horror" }); db.Categories.Add(new Category() { Name = "Humor" }); db.Categories.Add(new Category() { Name = "Science Fiction" }); db.Categories.Add(new Category() { Name = "Romance" }); db.SaveChanges(); } if (db.Books.Count() == 0) { var horror = db.Categories.FirstOrDefault(h => h.Name == "Horror"); var scienceFiction = db.Categories.FirstOrDefault(h => h.Name == "Science Fiction"); var humor = db.Categories.FirstOrDefault(h => h.Name == "Humor"); db.Books.Add(new Book() { Title = "Cabal (Nightbreed)", Author = "Clive Barker", Price = 250, Amount = 3, Categories = new List <Category>() { horror } }); db.Books.Add(new Book() { Title = "The Shining", Author = "Stephen King", Price = 200, Amount = 2, Categories = new List <Category>() { horror } }); db.Books.Add(new Book() { Title = "Doctor Sleep", Author = "Stephen King", Price = 200, Amount = 1, Categories = new List <Category>() { horror } }); db.Books.Add(new Book() { Title = "I Robot", Author = "Isaac Asimov", Price = 150, Amount = 4, Categories = new List <Category>() { scienceFiction } }); db.Books.Add(new Book() { Title = "Hitchhiker's guide to the galaxy", Author = "Douglas Adams", Price = 350, Amount = 3, Categories = new List <Category>() { humor, scienceFiction } }); db.SaveChanges(); } } }