Esempio n. 1
0
        /// <summary>
        /// Anonymize a question
        /// </summary>
        /// <param name="question">Question to anonymize</param>
        /// <param name="user">Concerned user</param>
        /// <param name="bid">Bid value</param>
        /// <returns>True if the process is ok</returns>
        public static bool AnonymiseQuestion(
            QuestionYourFriendsDataAccess.Question question,
            QuestionYourFriendsDataAccess.User user,
            int bid)
        {
            bool check = question != null && user != null;

            if (!check)
                return false;

            // Get the user and check his wallet
            if (user.credit_amount < bid)
            {
                _logger.ErrorFormat("You are out of cash: {0} it costs: {1}", user.credit_amount, bid);
                throw new ApplicationException(string.Format("You run out of cash! It costs {1} credits but you have only {0} credit left!", user.credit_amount, bid));
            }

            // Creation of the transaction
            check = Create(-bid, user.id, TransacType.Anonymize, question.id) != -1;

            // Update Question's price
            if (check)
                question.anom_price = bid;

            return check;
        }
Esempio n. 2
0
 /// <summary>
 /// Adds a transaction
 /// </summary>
 /// <param name="transac">Transaction to Add</param>
 /// <returns>The id of the created transaction</returns>
 public static int Create(QuestionYourFriendsDataAccess.Transac transac)
 {
     if (transac == null)
         return -1;
     var res = QuestionYourFriendsDataAccess.DataAccess.Transac.Create(Context.QyfEntities, transac);
     User.UpdateMoney(transac.userId);
     return res;
 }
Esempio n. 3
0
 /// <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.");
     }
 }
Esempio n. 4
0
 /// <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.");
     }
 }
Esempio n. 5
0
 /// <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.");
     }
 }
Esempio n. 6
0
 /// <summary>
 /// Deletes an user
 /// </summary>
 /// <param name="user">User to delete</param>
 /// <returns>True if the deletion is ok</returns>
 public static bool Delete(QuestionYourFriendsDataAccess.User user)
 {
     return QuestionYourFriendsDataAccess.DataAccess.User.Delete(Context.QyfEntities, user);
 }
Esempio n. 7
0
 /// <summary>
 /// Adds an user
 /// </summary>
 /// <param name="user">User to add</param>
 /// <returns>Id of the created user</returns>
 public static int Create(QuestionYourFriendsDataAccess.User user)
 {
     return QuestionYourFriendsDataAccess.DataAccess.User.Create(Context.QyfEntities, user);
 }
Esempio n. 8
0
        /// <summary>
        /// Updates a transaction
        /// </summary>
        /// <param name="qyfEntities">Entity context</param>
        /// <param name="transac">The transaction to update</param>
        /// <returns>True if the update is ok</returns>
        public static bool Update(QuestionYourFriendsEntities qyfEntities, QuestionYourFriendsDataAccess.Transac transac)
        {
            try
            {
                QuestionYourFriendsDataAccess.Transac transacFound =
                    qyfEntities.Transacs.Where(x => x.id == transac.id).FirstOrDefault();
                if (transacFound != null)
                {
                    _logger.InfoFormat("Transaction update: amount({0}), userId({1}), type({2})",
                        transacFound.amount, transacFound.User.id, transacFound.type);
                    transacFound.amount = transac.amount;
                    transacFound.status = transac.status;
                    transacFound.User = transac.User;
                    transacFound.type = transac.type;
                    transacFound.Question = transac.Question;
                    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.");
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Updates an user
        /// </summary>
        /// <param name="qyfEntities">Entity context</param>
        /// <param name="u">User to update</param>
        /// <returns>True if the update is ok</returns>
        public static bool Update(QuestionYourFriendsEntities qyfEntities, QuestionYourFriendsDataAccess.User u)
        {
            try
            {
                QuestionYourFriendsDataAccess.User userFound =
                    qyfEntities.Users.Where(x => x.id == u.id).FirstOrDefault();
                if (userFound != null)
                {
                    _logger.InfoFormat("User update: id({0}), fid({1}), credit({2}), activ({3})", u.id, u.fid, u.credit_amount, u.activated);
                    userFound.fid = u.fid;
                    userFound.credit_amount = u.credit_amount;
                    userFound.activated = u.activated;
                    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 update an user", ex);
                throw new ApplicationException("A database error occured during the operation.");
            }
        }
Esempio n. 10
0
 public static int GetNumberOfResponseToday(QuestionYourFriendsDataAccess.User user)
 {
     var questions = Question.GetList();
     return questions.Count(q => q.Receiver.id == user.id && q.date_answer.HasValue
         && q.date_answer.Value.Date == DateTime.Today);
 }
Esempio n. 11
0
 /// <summary>
 /// Updates a transaction
 /// </summary>
 /// <param name="transac">The transaction to updates</param>
 /// <returns>True if the update is ok</returns>
 public static bool Update(QuestionYourFriendsDataAccess.Transac transac)
 {
     return QuestionYourFriendsDataAccess.DataAccess.Transac.Update(Context.QyfEntities, transac);
 }
Esempio n. 12
0
 /// <summary>
 /// Get earning 
 /// </summary>
 /// <param name="user">Concerned user</param>
 /// <param name="amount">Concerned Amount</param>
 /// <returns>True if the process is ok</returns>
 public static bool Earning(QuestionYourFriendsDataAccess.User user, int amount)
 {
     return Create(amount, user.id, TransacType.Purchase, null) != -1;
 }
Esempio n. 13
0
 /// <summary>
 /// Purchase credit
 /// </summary>
 /// <param name="user">Concerned user</param>
 /// <param name="amount">Amount value</param>
 /// <returns>True if the process is ok</returns>
 public static bool Purchase(
     QuestionYourFriendsDataAccess.User user,
     int amount)
 {
     // Creation of the transaction
     return Create(amount, user.id, TransacType.Purchase, 0) != -1;
 }
Esempio n. 14
0
        /// <summary>
        /// Unprivatize and unanonymize a question
        /// </summary>
        /// <param name="question">Concerned question</param>
        /// <param name="user">Concerned user</param>
        /// <returns>True if the process is ok</returns>
        public static bool SpendAndQuestion(
            QuestionYourFriendsDataAccess.Question question,
            QuestionYourFriendsDataAccess.User user)
        {
            // Creation of the transaction
            int transacId = Create(-question.anom_price, user.id, TransacType.Anonymize, question.id);
            bool check = transacId != -1;
            if (check)
            {
                transacId = Create(-question.private_price, user.id, TransacType.Privatize, question.id);
                check &= transacId != -1;
            }
            else
            {
                var transac = Get(transacId);
                transac.SetTransacStatus(TransacStatus.Ko);
                Update(transac);
            }

            return check;
        }
Esempio n. 15
0
 /// <summary>
 /// Get earning from the beginning
 /// </summary>
 /// <param name="user">Concerned user</param>
 /// <returns>True if the process is ok</returns>
 public static bool EarningStartup(
     QuestionYourFriendsDataAccess.User user)
 {
     // Creation of the transaction
     return Create(QyfData.EarningStartup, user.id, TransacType.EarningStartup, 0) != -1;
 }
Esempio n. 16
0
 /// <summary>
 /// Unprivatize and unanonymize a question
 /// </summary>
 /// <param name="question">Concerned question</param>
 /// <param name="user">Concerned user</param>
 /// <returns>True if the process is ok</returns>
 public static bool SpendAndQuestion(
     QuestionYourFriendsDataAccess.Question question,
     QuestionYourFriendsDataAccess.User user)
 {
     // Creation of the transaction
     if (question.anom_price != 0)
         Create(-question.anom_price, user.id, TransacType.Anonymize, question.id);
     if (question.private_price != 0)
         Create(-question.private_price, user.id, TransacType.Privatize, question.id);
     EarningMessage(user);
     return true;
 }
Esempio n. 17
0
        /// <summary>
        /// Privatize a question
        /// </summary>
        /// <param name="question">Question to privatize</param>
        /// <param name="user">Concerned user</param>
        /// <param name="bid">Bid value</param>
        /// <returns>True if the process is ok</returns>
        public static bool PrivatizeQuestion(
            QuestionYourFriendsDataAccess.Question question,
            QuestionYourFriendsDataAccess.User user,
            int bid)
        {
            if (question == null || user == null)
                return false;

            // Get the user and check his wallet
            if (user.credit_amount < bid)
            {
                _logger.ErrorFormat("You are out of cash: {0} it costs: {1}", user.credit_amount, bid);
                throw new ApplicationException(string.Format("You run out of cash! It costs {1} credits but you have only {0} credit left!", user.credit_amount, bid));
            }

            // Creation of the transaction
            Create(-bid, question.Owner.id, TransacType.Privatize, question.id);

            // Update Question's price
            question.private_price = bid;
            Question.Update(question);
            return true;
        }
Esempio n. 18
0
 /// <summary>
 /// Adds a question
 /// </summary>
 /// <param name="question">Question to add</param>
 /// <returns>The id of the newly created question</returns>
 public static int Create(QuestionYourFriendsDataAccess.Question question)
 {
     return QuestionYourFriendsDataAccess.DataAccess.Question.Create(Context.QyfEntities, question);
 }
Esempio n. 19
0
 /// <summary>
 /// Get earning from asking a question
 /// </summary>
 /// <param name="user">Concerned user</param>
 /// <returns>True if the process is ok</returns>
 public static bool EarningMessage(
     QuestionYourFriendsDataAccess.User user)
 {
     // Creation of the transaction
     if (GetNumberOfQuestionToday(user) < QyfData.FreeQuestionPerDay)
         return Create(QyfData.EarningMessage, user.id, TransacType.EarningAnswer, null) != -1;
     return false;
 }
Esempio n. 20
0
 /// <summary>
 /// Deletes a question
 /// </summary>
 /// <param name="question">Question to delete</param>
 /// <returns>True if the deletion is ok</returns>
 public static bool Delete(QuestionYourFriendsDataAccess.Question question)
 {
     return QuestionYourFriendsDataAccess.DataAccess.Question.Delete(Context.QyfEntities, question);
 }
Esempio n. 21
0
        /// <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.");
            }
        }
Esempio n. 22
0
 /// <summary>
 /// Get earning from an answer
 /// </summary>
 /// <param name="user">Concerned user</param>
 /// <returns>True if the process is ok</returns>
 public static bool EarningAnswer(
     QuestionYourFriendsDataAccess.User user)
 {
     // Creation of the transaction
     return Create(QyfData.EarningAnswer, user.id, TransacType.EarningAnswer, null) != -1;
 }
Esempio n. 23
0
 public static int GetNumberOfQuestionToday(QuestionYourFriendsDataAccess.User user)
 {
     var questions = Question.GetList();
     return questions.Count(q => q.Owner.id == user.id && q.date_pub.Date == DateTime.Today);
 }