/// <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> /// 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 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 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 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.id_owner, q.id_receiver, q.text, q.anom_price, q.private_price, q.date_pub); qyfEntities.Questions.AddObject(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 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> /// 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> /// 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.User.id, 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> /// 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."); } }