<commit_before>﻿using Favor.Data;
using Favor.Models.FavorModels;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Favor.Controllers;

namespace Favor.Controllers
{
    public class FavorTradeController : Controller
    {
        [Authorize]
        public ActionResult AcceptTrade(int? id)
        {
            if (!ParamsAreValid(id))
            {
                return RedirectToAction("Profile", "Profile");
            }

            var db = new FavorDbContext();

            var favorTradeModel = db.FavorTradeModels.Find(id);
            var fullFavor = db.Favors.Find(favorTradeModel.FavorId);

            if (!ParamsAreValid(favorTradeModel, fullFavor))
            {
                return RedirectToAction("Profile", "Profile");
            }

            var receiverUser = db.Users.Include(u => u.PendingFavors).Include(u => u.AccomplishedFavors).FirstOrDefault(u => u.Id == favorTradeModel.RecieverId);
            var senderUser = db.Users.Include(u => u.SentFavors).Include(u => u.AccomplishedFavors).FirstOrDefault(u => u.Id == favorTradeModel.SenderId);

            var accomplishedTradeModelForTheReceiver = new AccomplishedTradeModel
            {
                FavorTitle = favorTradeModel.FavorTitle,
                FavorContent = favorTradeModel.FavorContent,
                TradeOff = favorTradeModel.TradeOff,
                SenderId = favorTradeModel.SenderId,
                SenderFullName = favorTradeModel.SenderFullName,
                ReceiverId = favorTradeModel.RecieverId,
                ReceiverFullName = favorTradeModel.RecieverFullName
            };

            var accomplishedTradeForTheSender = AccomplishedTradeModel.ToAccomplishedTradeModel(accomplishedTradeModelForTheReceiver);

            if (!ParamsAreValid(receiverUser, senderUser))
            {
                return RedirectToAction("Profile", "Profile");
            }

            List<FavorTradeModel> allFavorTradeModels = db.FavorTradeModels.Where(ftm => ftm.FavorId == favorTradeModel.FavorId).ToList();

            for (int currFavorTradeModel = 0; currFavorTradeModel < allFavorTradeModels.Count; currFavorTradeModel++)
            {
                db.FavorTradeModels.Remove(allFavorTradeModels[currFavorTradeModel]);
            }
            
            receiverUser.AccomplishedFavors.Add(accomplishedTradeModelForTheReceiver);
            senderUser.AccomplishedFavors.Add(accomplishedTradeForTheSender);

            db.Favors.Remove(fullFavor);

            db.SaveChanges();

            return RedirectToAction("Profile", "Profile");
        }

        [Authorize]
        public ActionResult CancelTrade(int? id)
        {
            if (!ParamsAreValid(id))
            {
                return RedirectToAction("Profile", "Profile");
            }

            var db = new FavorDbContext();
<commit_msg>Now the system sends messages to all the users.<commit_after>﻿using Favor.Data;
using Favor.Models.FavorModels;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Favor.Controllers;

namespace Favor.Controllers
{
    public class FavorTradeController : Controller
    {
        [Authorize]
        public ActionResult AcceptTrade(int? id)
        {
            if (!ParamsAreValid(id))
            {
                return RedirectToAction("Profile", "Profile");
            }

            var db = new FavorDbContext();

            var favorTradeModel = db.FavorTradeModels.Find(id);
            var fullFavor = db.Favors.Find(favorTradeModel.FavorId);

            if (!ParamsAreValid(favorTradeModel, fullFavor))
            {
                return RedirectToAction("Profile", "Profile");
            }

            var receiverUser = db.Users.Include(u => u.PendingFavors).Include(u => u.AccomplishedFavors).FirstOrDefault(u => u.Id == favorTradeModel.RecieverId);
            var senderUser = db.Users.Include(u => u.SentFavors).Include(u => u.AccomplishedFavors).FirstOrDefault(u => u.Id == favorTradeModel.SenderId);

            var accomplishedTradeModelForTheReceiver = new AccomplishedTradeModel
            {
                FavorTitle = favorTradeModel.FavorTitle,
                FavorContent = favorTradeModel.FavorContent,
                TradeOff = favorTradeModel.TradeOff,
                SenderId = favorTradeModel.SenderId,
                SenderFullName = favorTradeModel.SenderFullName,
                ReceiverId = favorTradeModel.RecieverId,
                ReceiverFullName = favorTradeModel.RecieverFullName
            };

            var accomplishedTradeForTheSender = AccomplishedTradeModel.ToAccomplishedTradeModel(accomplishedTradeModelForTheReceiver);

            if (!ParamsAreValid(receiverUser, senderUser))
            {
                return RedirectToAction("Profile", "Profile");
            }

            List<FavorTradeModel> allFavorTradeModels = db.FavorTradeModels
                                                            .Where(ftm => ftm.FavorId == favorTradeModel.FavorId &&
                                                                          ftm.SenderId != favorTradeModel.SenderId)
                                                            .ToList();

            MessageController test = new MessageController();

            for (int currFavorTradeModel = 0; currFavorTradeModel < allFavorTradeModels.Count; currFavorTradeModel++)
            {
                var user = db.Users.Find(allFavorTradeModels[currFavorTradeModel].RecieverId);

                Message cancelMessage = new Message
                {
                    ReceiverId = allFavorTradeModels[currFavorTradeModel].SenderId,
                    SenderEmail = user.Email,
                    Title = favorTradeModel.FavorTitle,
                    Content = "The trade favor with this title was canceled",
                    Type = MessageType.System
                };

                test.SendMessage(cancelMessage);

                db.FavorTradeModels.Remove(allFavorTradeModels[currFavorTradeModel]);
            }
            
            receiverUser.AccomplishedFavors.Add(accomplishedTradeModelForTheReceiver);
            senderUser.AccomplishedFavors.Add(accomplishedTradeForTheSender);

            Message successMessage = new Message
            {
                ReceiverId = receiverUser.Id,
                SenderEmail = senderUser.Email,
                Title = accomplishedTradeForTheSender.FavorTitle,
                Content = "The trade favor with this title was accepted!!!",
                Type = MessageType.System
            };
            
            test.SendMessage(successMessage);

            allFavorTradeModels = db.FavorTradeModels
                                       .Where(ftm => ftm.FavorId == favorTradeModel.FavorId)
                                       .ToList();

            for (int currFavorTradeModel = 0; currFavorTradeModel < allFavorTradeModels.Count; currFavorTradeModel++)
            {
                db.FavorTradeModels.Remove(allFavorTradeModels[currFavorTradeModel]);
            }

            db.Favors.Remove(fullFavor);

            db.SaveChanges();

            return RedirectToAction("Profile", "Profile");
        }

        [Authorize]
        public ActionResult CancelTrade(int? id)
        {
            if (!ParamsAreValid(id))
            {
                return RedirectToAction("Profile", "Profile");
            }

            var db = new FavorDbContext();

{'fraction_non_alphanumeric': 0.0831270358306189, 'fraction_numerical': 0.00039087947882736156, 'mean_word_length': 1.974041069352964, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4433073', 'n_tokens_mistral': 2111, 'n_tokens_neox': 2036, 'n_words': 331}