/// <summary> /// Adds a transaction /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="amount">Amount of the transaction</param> /// <param name="userId">Id of the owner</param> /// <param name="type">Type of the transaction</param> /// <param name="questionId">Id of the question</param> /// <returns>The id of the created transaction</returns> public static int Create(QuestionYourFriendsEntities qyfEntities, int amount, int userId, TransacType type, int? questionId) { try { _logger.InfoFormat("New transaction creation: amount({0}), userId({1}), type({2})", amount, userId, type); var transac = new QuestionYourFriendsDataAccess.Transac { UserReference = {EntityKey = new EntityKey("QuestionYourFriendsEntities.Users", "id", userId)} }; transac.SetTransacStatus(TransacStatus.Ok); transac.SetTransacType(type); if (questionId.HasValue) transac.QuestionReference.EntityKey = new EntityKey("QuestionYourFriendsEntities.Questions", "id", questionId); qyfEntities.AddToTransacs(transac); transac.amount = amount; qyfEntities.SaveChanges(); _logger.InfoFormat("New transaction id: {0}", transac.id); return transac.id; } catch (Exception ex) { _logger.Error("Cannot create a new transaction", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Adds a question /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="idOwner">User id of the owner</param> /// <param name="idReceiver">User id of the receiver</param> /// <param name="text">Question content</param> /// <param name="anonPrice">Price to anonymize</param> /// <param name="privatePrice">Price to privatize</param> /// <param name="datePub">Publication date</param> /// <returns>The id of the created Question</returns> public static int Create(QuestionYourFriendsEntities qyfEntities, int idOwner, int idReceiver, string text, int anonPrice, int privatePrice, DateTime datePub) { try { _logger.InfoFormat("New question creation: owner({0}), receiver({1}), text({2}), anon({3}), priv({4}), datePub({5})", idOwner, idReceiver, text, anonPrice, privatePrice, datePub); QuestionYourFriendsDataAccess.Question question = qyfEntities.Questions.CreateObject(); question.id_owner = idOwner; question.id_receiver = idReceiver; question.text = text; question.answer = null; question.anom_price = anonPrice; question.private_price = privatePrice; question.undesirable = false; question.date_pub = datePub; question.date_answer = null; question.deleted = false; qyfEntities.Questions.AddObject(question); qyfEntities.SaveChanges(); _logger.InfoFormat("New question id: {0}", question.id); return question.id; } catch (Exception ex) { _logger.Error("Cannot create a new question", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Deletes an user thanks to its fid /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="fid">User's fid to delete</param> /// <returns>True if the deletion is ok</returns> public static bool Delete(QuestionYourFriendsEntities qyfEntities, long fid) { try { QuestionYourFriendsDataAccess.User userFound = qyfEntities.Users.Where(x => x.fid == fid).FirstOrDefault(); if (userFound != null) { _logger.InfoFormat("User deletion: id ({0}), fid({1})", userFound.id, fid); userFound.activated = false; try { qyfEntities.SaveChanges(); } catch (Exception e) { _logger.Error("Context error:", e); // Resolve the concurrency conflict by refreshing the // object context before re-saving changes. qyfEntities.Refresh(RefreshMode.ClientWins, userFound); // Save changes. qyfEntities.SaveChanges(); } return true; } return false; } catch (Exception ex) { _logger.Error("Cannot delete an user", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Adds a transaction /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="t">Transaction to add</param> /// <returns>Id of the created transaction</returns> public static int Create(QuestionYourFriendsEntities qyfEntities, QuestionYourFriendsDataAccess.Transac t) { try { _logger.InfoFormat("New transaction creation: amount({0}), userId({1}), type({2})", t.amount, t.User.id, t.type); qyfEntities.AddToTransacs(t); qyfEntities.SaveChanges(); _logger.InfoFormat("New transaction id: {0}", t.id); return t.id; } catch (Exception ex) { _logger.Error("Cannot create a new transaction", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Adds an user /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="user">User to add</param> /// <returns>The id of the created user</returns> public static int Create(QuestionYourFriendsEntities qyfEntities, QuestionYourFriendsDataAccess.User user) { try { _logger.InfoFormat("New user creation: fid({0})", user.fid); qyfEntities.AddToUsers(user); qyfEntities.SaveChanges(); _logger.InfoFormat("New user id: {0}", user.id); return user.id; } catch (Exception ex) { _logger.Error("Cannot create a new user", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Adds a question /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="q">Question to add</param> /// <returns>The id of the created Question</returns> public static int Create(QuestionYourFriendsEntities qyfEntities, QuestionYourFriendsDataAccess.Question q) { try { _logger.InfoFormat("New question creation: owner({0}), receiver({1}), text({2}), anon({3}), priv({4}), datePub({5})", q.Owner.id, q.Receiver.id, q.text, q.anom_price, q.private_price, q.date_pub); qyfEntities.AddToQuestions(q); qyfEntities.SaveChanges(); _logger.InfoFormat("New question id: {0}", q.id); return q.id; } catch (Exception ex) { _logger.Error("Cannot create a new question", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Adds an user from its fid /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="fid">User's fid</param> /// <returns>The id of the created user</returns> public static int Create(QuestionYourFriendsEntities qyfEntities, long fid) { try { _logger.InfoFormat("New user creation: fid({0})", fid); QuestionYourFriendsDataAccess.User user = qyfEntities.Users.CreateObject(); user.fid = fid; user.activated = true; user.credit_amount = 0; qyfEntities.Users.AddObject(user); qyfEntities.SaveChanges(); _logger.InfoFormat("New user id: {0}", user.id); return user.id; } catch (Exception ex) { _logger.Error("Cannot create a new user", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Adds a question /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="idOwner">User id of the owner</param> /// <param name="idReceiver">User id of the receiver</param> /// <param name="text">Question content</param> /// <param name="anonPrice">Price to anonymize</param> /// <param name="privatePrice">Price to privatize</param> /// <param name="datePub">Publication date</param> /// <returns>The id of the created Question</returns> public static int Create(QuestionYourFriendsEntities qyfEntities, int idOwner, int idReceiver, string text, int anonPrice, int privatePrice, DateTime datePub) { try { _logger.InfoFormat("New question creation: owner({0}), receiver({1}), text({2}), anon({3}), priv({4}), datePub({5})", idOwner, idReceiver, text, anonPrice, privatePrice, datePub); var question = new QuestionYourFriendsDataAccess.Question { OwnerReference = { EntityKey = new EntityKey("QuestionYourFriendsEntities.Users", "id", idOwner) }, ReceiverReference = { EntityKey = new EntityKey("QuestionYourFriendsEntities.Users", "id", idReceiver) }, text = text, answer = null, anom_price = anonPrice, private_price = privatePrice, undesirable = false, date_pub = datePub, date_answer = null, deleted = false }; qyfEntities.AddToQuestions(question); qyfEntities.SaveChanges(); _logger.InfoFormat("New question id: {0}", question.id); return question.id; } catch (Exception ex) { _logger.Error("Cannot create a new question", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Adds a transaction /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="amount">Amount of the transaction</param> /// <param name="userId">Id of the owner</param> /// <param name="type">Type of the transaction</param> /// <param name="questionId">Id of the question</param> /// <returns>The id of the created transaction</returns> public static int Create(QuestionYourFriendsEntities qyfEntities, int amount, int userId, TransacType type, int? questionId) { try { _logger.InfoFormat("New transaction creation: amount({0}), userId({1}), type({2})", amount, userId, type); QuestionYourFriendsDataAccess.Transac transac = qyfEntities.Transacs.CreateObject(); transac.amount = amount; transac.userId = userId; transac.SetTransacStatus(TransacStatus.Ok); transac.SetTransacType(type); transac.questionId = questionId; qyfEntities.Transacs.AddObject(transac); qyfEntities.SaveChanges(); _logger.InfoFormat("New transaction id: {0}", transac.id); return transac.id; } catch (Exception ex) { _logger.Error("Cannot create a new transaction", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Deletes the transaction thanks to its id /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="id">Transaction's id to delete</param> /// <returns>True if the deletion is ok</returns> public static bool Delete(QuestionYourFriendsEntities qyfEntities, int id) { try { QuestionYourFriendsDataAccess.Transac transacFound = qyfEntities.Transacs.Where(x => x.id == id).FirstOrDefault(); if (transacFound != null) { _logger.InfoFormat("Transaction deletion: amount({0}), userId({1}), type({2})", transacFound.amount, transacFound.userId, transacFound.type); transacFound.status = 0; try { qyfEntities.SaveChanges(); } catch (Exception e) { _logger.Error("Context error:", e); // Resolve the concurrency conflict by refreshing the // object context before re-saving changes. qyfEntities.Refresh(RefreshMode.ClientWins, transacFound); // Save changes. qyfEntities.SaveChanges(); } return true; } return false; } catch (Exception ex) { _logger.Error("Cannot update a transaction", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Get the list of questions of your friends /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="friends">array of friends id</param> /// <returns></returns> public static List<QuestionYourFriendsDataAccess.Question> GetFriendsQuestions(QuestionYourFriendsEntities qyfEntities, int[] friends) { try { _logger.Info("Get friends questions"); var res = qyfEntities.Questions .Include("Owner").Include("Receiver") .Where(x => friends.Contains(x.id_receiver) && x.deleted == false && x.undesirable == false && x.private_price == 0 && x.date_pub < DateTime.Now) .OrderBy(x => x.date_pub).ToList(); res.Reverse(); return res; } catch (Exception ex) { _logger.Error("Cannot get questions", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Recalculate credit amount from the transactions /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="id">Id of the user to update</param> /// <returns>True if the process is ok</returns> public static bool UpdateMoney(QuestionYourFriendsEntities qyfEntities, int id) { try { _logger.InfoFormat("Update money of user: id({0})", id); var user = qyfEntities.Users.Where(u => u.id == id).FirstOrDefault(); if (user == null) return false; var transacs = qyfEntities.Transacs .Where(t => t.User.id == id && t.status != (int)TransacStatus.Ko); int sum = Enumerable.Sum(transacs, transac => transac.amount); user.credit_amount = sum; try { qyfEntities.SaveChanges(); } catch (Exception e) { _logger.Error("Context error:", e); // Resolve the concurrency conflict by refreshing the // object context before re-saving changes. qyfEntities.Refresh(RefreshMode.ClientWins, user); // Save changes. qyfEntities.SaveChanges(); } return true; } catch (Exception ex) { _logger.Error("Cannot update money", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Gets a list of users thanks to their fids /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="fids">List of fids</param> /// <returns>The requested list of users</returns> public static IEnumerable<QuestionYourFriendsDataAccess.User> GetUsersFromFids(QuestionYourFriendsEntities qyfEntities, List<long> fids) { try { _logger.Info("Get users from fid"); var users = qyfEntities.Users.Include("QuestionsToMe").Include("MyQuestions").Include("Transacs"); var res = new List<QuestionYourFriendsDataAccess.User>(); foreach (var user in users) { if (fids.Contains(user.fid)) res.Add(user); } return res; } catch (Exception ex) { _logger.Error("Cannot get users", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Get the list of all users /// </summary> /// <param name="qyfEntities">Entity context</param> /// <returns>The list of all users</returns> public static List<QuestionYourFriendsDataAccess.User> GetList(QuestionYourFriendsEntities qyfEntities) { try { _logger.Info("Get users"); return qyfEntities.Users .Include("QuestionsToMe").Include("MyQuestions").Include("Transacs") .ToList(); } catch (Exception ex) { _logger.Error("Cannot get users", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Gets an user from its fid /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="fid">User's fid</param> /// <returns>The requested user</returns> public static QuestionYourFriendsDataAccess.User Get(QuestionYourFriendsEntities qyfEntities, long fid) { try { _logger.InfoFormat("Get user: fid({0})", fid); return qyfEntities.Users .Include("QuestionsToMe").Include("MyQuestions").Include("Transacs") .Where(x => x.fid == fid).FirstOrDefault(); } catch (Exception ex) { _logger.Error("Cannot get an user", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Gets an user from its id /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="id">User's id</param> /// <returns>The requested user</returns> public static QuestionYourFriendsDataAccess.User Get(QuestionYourFriendsEntities qyfEntities, int id) { try { _logger.InfoFormat("Get user: id({0})", id); return qyfEntities.Users.Where(x => x.id == id).FirstOrDefault(); } catch (Exception ex) { _logger.Error("Cannot get an user", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Get the list of questions of your friends /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="friends">array of friends id</param> /// <returns></returns> public static List<QuestionYourFriendsDataAccess.Question> GetFriendsQuestions(QuestionYourFriendsEntities qyfEntities, IEnumerable<int> friends) { try { _logger.Info("Get friends questions"); var questions = qyfEntities.Questions.Include("Owner").Include("Receiver"); var res = new List<QuestionYourFriendsDataAccess.Question>(); foreach (var q in questions) { if (friends.Contains(q.Receiver.id) && q.deleted == false && q.undesirable == false && q.private_price == 0 && q.date_pub < DateTime.Now) res.Add(q); } res.Sort(QyfComparer.Instance); return res; } catch (Exception ex) { _logger.Error("Cannot get questions", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Get list of all questions /// </summary> /// <param name="qyfEntities">Entity context</param> /// <returns>List of questions</returns> public static List<QuestionYourFriendsDataAccess.Question> GetList(QuestionYourFriendsEntities qyfEntities) { try { _logger.Info("Get questions"); return qyfEntities.Questions .Include("Owner").Include("Receiver") .ToList(); } catch (Exception ex) { _logger.Error("Cannot get questions", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Gets a transaction thanks to its id /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="id">The id of the transaction</param> /// <returns>The requested transaction</returns> public static QuestionYourFriendsDataAccess.Transac Get(QuestionYourFriendsEntities qyfEntities, long id) { try { _logger.InfoFormat("Get transaction: id({0})", id); return qyfEntities.Transacs.Include("Question").Include("User") .Where(x => x.id == id).FirstOrDefault(); } catch (Exception ex) { _logger.Error("Cannot get a transaction", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Gets a list of users thanks to their fids /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="fids">List of fids</param> /// <returns>The requested list of users</returns> public static IEnumerable<QuestionYourFriendsDataAccess.User> GetUsersFromFids(QuestionYourFriendsEntities qyfEntities, IEnumerable<long> fids) { try { _logger.Info("Get users from fid"); return qyfEntities.Users .Include("QuestionsToMe").Include("MyQuestions").Include("Transacs") .Where(x => fids.Contains(x.fid)).ToList(); } catch (Exception ex) { _logger.Error("Cannot get users", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Get the list of receivers /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="id">Receiver id</param> /// <returns>List of questions where we are receiver</returns> public static List<QuestionYourFriendsDataAccess.Question> GetListOfReceiver(QuestionYourFriendsEntities qyfEntities, int id) { try { _logger.InfoFormat("Get my questions: id({0})", id); var res = qyfEntities.Questions .Include("Owner").Include("Receiver"). Where(x => x.Receiver.id == id && x.deleted == false && x.undesirable == false && x.date_pub < DateTime.Now) .ToList(); res.Sort(QyfComparer.Instance); return res; } catch (Exception ex) { _logger.Error("Cannot get questions", ex); throw new ApplicationException("A database error occured during the operation."); } }
/// <summary> /// Updates a question /// </summary> /// <param name="qyfEntities">Entity context</param> /// <param name="q">Question to update</param> /// <returns>True if the update is ok</returns> public static bool Update(QuestionYourFriendsEntities qyfEntities, QuestionYourFriendsDataAccess.Question q) { try { QuestionYourFriendsDataAccess.Question questionFound = qyfEntities.Questions.Where(x => x.id == q.id).FirstOrDefault(); if (questionFound != null) { _logger.InfoFormat("Question update: owner({0}), receiver({1}), text({2}), anon({3}), priv({4}), datePub({5})", q.Owner.id, q.Receiver.id, q.text, q.anom_price, q.private_price, q.date_pub); questionFound.Owner = q.Owner; questionFound.Receiver = q.Receiver; questionFound.text = q.text; questionFound.answer = q.answer; questionFound.anom_price = q.anom_price; questionFound.private_price = q.private_price; questionFound.undesirable = q.undesirable; questionFound.date_pub = q.date_pub; questionFound.date_answer = q.date_answer; questionFound.deleted = q.deleted; try { qyfEntities.SaveChanges(); } catch (Exception e) { _logger.Error("Context error:", e); // Resolve the concurrency conflict by refreshing the // object context before re-saving changes. qyfEntities.Refresh(RefreshMode.ClientWins, questionFound); // Save changes. qyfEntities.SaveChanges(); } return true; } return false; } catch (Exception ex) { _logger.Error("Cannot update a question", ex); throw new ApplicationException("A database error occured during the operation."); } }