Example #1
0
        /// <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.");
            }
        }
Example #2
0
        /// <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.");
            }
        }
Example #3
0
        /// <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.");
            }
        }
Example #4
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.");
            }
        }