/// <summary> /// Attempts to log in using specified user name and password. /// Checks Active and Created users. /// If the state is Created changes state to Active. /// </summary> /// <param name="userName">Username to be checked.</param> /// <param name="password">Password to be checked.</param> /// <returns>If operation succeded returns new User object. /// Otherwise returns null</returns> public async Task <User> LogIn(string userName, string password) { var user = await context.Users.FirstOrDefaultAsync( x => x.Name == userName && // search by userName (x.State == 0 || x.State == 1)); //user 'created' or 'active' if (user == null) { return(null); } if (user.Password != Hash(password)) { return(null); } if (user.State == 0) { user.State = 1; context.Users.Attach(user); var entry = context.Entry(user); entry.Property(e => e.State).IsModified = true; await context.SaveChangesAsync(); } User loggedUserData = new User( user.Name, user.E_mail, user.Type, user.State); loggedUserData.UserID = user.UserID; return(loggedUserData); }
/// <summary> /// Sets Welcome Message in database. /// </summary> /// <param name="newMessage">A new message.</param> /// <returns>True if succeded, false otherwise.</returns> public async Task <bool> SetWelcomeMessage(string newMessage) { var message = await context.Other.FirstOrDefaultAsync(); if (message == null) { Other dbOther = new Other(); dbOther.Welcome_message = newMessage; dbOther.Contact_info = ""; context.Other.Add(dbOther); await context.SaveChangesAsync(); } else { message.Welcome_message = newMessage; context.Entry(message).Property(e => e.Welcome_message).IsModified = true; await context.SaveChangesAsync(); } return(true); }
///// <summary> ///// Saves a basket ///// </summary> ///// <param name="basket">Basket object.</param> ///// <returns>True if could save, false otherwise.</returns> //public async Task<Basket> AddBasket(Basket basket) //{ // var basketEntity = new Baskets(); // basketEntity.UserID = basket.UserID; // basketEntity.ProductID = basket.ProductID; // basketEntity.Amount = basket.Amount; // basketEntity.Date = basket.Date; // context.Baskets.Add(basketEntity); // await context.SaveChangesAsync(); // basket.BasketID = basketEntity.BasketID; // return basket; //} /// <summary> /// Moves the specified amount of specified product to the basket of specified user. /// </summary> /// <param name="UserID">Specifies user</param> /// <param name="ProductID">Specifies product</param> /// <param name="Amount">Specifies amount</param> /// <returns>True if could complete operation, false otherwise.</returns> public async Task <bool> MoveProductToBasket(long UserID, long ProductID, decimal Amount) { if (Amount <= 0) { return(false); } var product = await context.Products.FindAsync(ProductID); if (product == null) { return(false); } if (Amount > product.Amount) { return(false); } var basket = await context.Baskets.FirstOrDefaultAsync(x => x.UserID == UserID && x.ProductID == ProductID); if (basket == null) { basket = new Baskets(); basket.Amount = Amount; basket.Date = DateTime.Now; basket.ProductID = ProductID; basket.UserID = UserID; context.Baskets.Add(basket); } else { basket.Amount += Amount; context.Entry(basket).Property(x => x.Amount).IsModified = true; basket.Date = DateTime.Now; } product.Amount -= Amount; context.Entry(product).Property(x => x.Amount).IsModified = true; // ok, problem jest taki, że zmieniamy basket i product, tylko, że zmieniamy lokalną zmienną chyba, a nie w samej kolekcji i to trzeba sfiksować await context.SaveChangesAsync(); return(true); }
/// <summary> /// Changes the state of a specified product in database. /// If product state is Active, sets him as Archival. /// If product state is Created, CRUSH IT! SMASH, TEAR, RIP. /// If product state is Archival, does nothing. /// </summary> /// <param name="productName">Product name of the product we want to modify.</param> /// <returns>True if success, false otherwise.</returns> public async Task <bool> DeleteProduct(string productName) { var dbProduct = await context.Products.FirstOrDefaultAsync( x => x.Name == productName && (x.State == 0 || x.State == 1) ); if (dbProduct == null) { return(false); } switch (dbProduct.State) { case 0: //created, can be annihilated, and so its swarms in baskets { context.Products.Remove(dbProduct); var baskets = await context.Baskets.Where(x => x.ProductID == dbProduct.ProductID).ToListAsync(); foreach (var basket in baskets) { context.Baskets.Remove(basket); } await context.SaveChangesAsync(); return(true); } case 1: //active, must be archived { dbProduct.State = 2; context.Entry(dbProduct).Property(e => e.State).IsModified = true; await context.SaveChangesAsync(); return(true); } } return(false); }