/// <summary> /// Login operation /// Create a session for user if auth is Ok. /// </summary> /// <param name="login"></param> /// <param name="password"></param> /// <returns></returns> public static AuthenticationResponse Login(string login, string password) { login = login.Trim(); password = password.Trim(); Users user = MongoManager.FirstOrDefault <Users>(x => x.Login == login); if (user == null) { throw new Exception($"There is no user {login} in the Database."); } if (user.Password != password) { throw new Exception("The password is wrong."); } Sessions session = MongoManager.SingleOrDefault <Sessions>(x => x.UserId == user._id); if (session == null) { session = new Sessions(Crypto.GenerateUniqueKey(), user._id); MongoManager.AddItemInCollection(session); } log.Information($"Added session {session._id}."); return(new AuthenticationResponse(user, session)); }
/// <summary> /// Check session and returns some related objects. /// </summary> /// <param name="sessionKey"></param> /// <returns></returns> public static AuthenticationResponse AuthenticateSession(string sessionKey) { // Get session Sessions session = MongoManager.FirstOrDefault <Sessions>(s => s.Key == sessionKey); if (session == null) { // BAD auth log.Error("Auth FAILED, fail to get the fetched database row."); return(BaseResponse <AuthenticationResponse> .BadAuth()); } // OK, session existing log.Information($"Auth OK, for userId = [{session.UserId}]."); return(new AuthenticationResponse(MongoManager.First <Users>(x => x._id == session.UserId), session)); }
/// <summary> /// Get the ingredient by its id. /// </summary> /// <param name="id"></param> /// <returns></returns> public static Ingredients Get(ObjectId id) => MongoManager.FirstOrDefault <Ingredients>(x => x._id == id);
/// <summary> /// Get the recipe by its id and userId. /// </summary> /// <param name="id"></param> /// <param name="userId"></param> /// <returns></returns> public static Recipes Get(ObjectId id, ObjectId userId) => MongoManager.FirstOrDefault <Recipes>(x => x._id == id && x.UserId == userId);
/// <summary> /// Get the ingredient price by the ingredientId and userId. /// </summary> /// <param name="ingredientId">Id of the ingredient.</param> /// <param name="userId">Id of the user.</param> /// <returns></returns> public static IngredientPrices Get(ObjectId ingredientId, ObjectId userId) => MongoManager.FirstOrDefault <IngredientPrices>(x => x.IngredientId == ingredientId && x.UserId == userId);