Esempio n. 1
0
        public ActionResult Edit(FavorEditViewModel favorEditViewModel)
        {
            if (favorEditViewModel == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var db = new FavorDbContext();

            var fullFavor = db.Favors.FirstOrDefault(f => f.Id == favorEditViewModel.Id);

            if (fullFavor == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            fullFavor.Title         = favorEditViewModel.Title;
            fullFavor.Description   = favorEditViewModel.Description;
            fullFavor.PayOff        = favorEditViewModel.PayOff;
            fullFavor.FavorType     = favorEditViewModel.FavorType;
            fullFavor.FavorCategory = favorEditViewModel.Category;

            db.Entry(fullFavor).State = EntityState.Modified;
            db.SaveChanges();

            return(RedirectToAction("Details", new { @id = fullFavor.Id }));
        }
Esempio n. 2
0
        public ActionResult Delete(FavorDeleteViewModel favorDeleteViewModel)
        {
            if (favorDeleteViewModel == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var db        = new FavorDbContext();
            var fullFavor = db.Favors.FirstOrDefault(f => f.Id == favorDeleteViewModel.Id);

            if (fullFavor == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var allUsers = db.Users.Include(u => u.SentFavors).Include(u => u.PendingFavors).Include(u => u.Messages).ToList();

            DeleteAllPendingAndSentRequestsFromUsersAccounts(allUsers, favorDeleteViewModel.Id);

            db.Favors.Remove(fullFavor);

            db.SaveChanges();

            return(RedirectToAction("Index", "Home"));
        }
        public ActionResult DeleteAllMessages(int?profileIntegerId)
        {
            if (profileIntegerId == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var db = new FavorDbContext();

            var user = db.Users
                       .Include(u => u.Messages)
                       .Include(u => u.TicketsForSender)
                       .Include(u => u.TicketsForAdmin)
                       .FirstOrDefault(u => u.IntegerId == profileIntegerId);

            if (user == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            user.Messages.Clear();
            user.TicketsForAdmin.Clear();
            user.TicketsForSender.Clear();

            db.SaveChanges();

            return(RedirectToAction("Profile", "Profile"));
        }
        public ActionResult SendTicket(string title, string content, string senderEmail)
        {
            if (this.User.IsInRole("Admin") ||
                senderEmail == null ||
                title == null ||
                content == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var db = new FavorDbContext();

            List <User> allAdmins = new List <User>();

            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db));
            var adminRole   = roleManager.FindByName("Admin");

            allAdmins = db.Users.Where(x => x.Roles.Any(s => s.RoleId == adminRole.Id)).ToList();

            if (allAdmins.Count == 0)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var senderUser = db.Users.FirstOrDefault(u => u.Email == senderEmail);

            var ticketForSender = new TicketForSender
            {
                Title   = title,
                Content = content
            };

            senderUser.TicketsForSender.Add(ticketForSender);
            senderUser.Notifications.FromMessages += 1;

            foreach (var user in allAdmins)
            {
                var ticketForAdmin = new TicketForAdmin
                {
                    SenderId       = senderUser.Id,
                    SenderFullName = senderUser.FullName,
                    RecieverEmail  = user.Email,
                    Title          = title,
                    Content        = content,
                };

                user.TicketsForAdmin.Add(ticketForAdmin);
                user.Notifications.FromMessages += 1;
            }

            db.SaveChanges();

            MessageManager.TicketHasBeenSent = true;

            return(RedirectToAction("Contact", "Home"));
        }
        public ActionResult SendMessage(Message sentMessage)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var db = new FavorDbContext();

            var receiverUser = db.Users.Include(u => u.Messages).FirstOrDefault(u => u.Id == sentMessage.ReceiverId);
            var senderUser   = db.Users.Include(u => u.Messages).FirstOrDefault(u => u.UserName == sentMessage.SenderEmail);

            if (receiverUser == null || senderUser == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            MessageType forTheReciever = MessageType.Received;

            if (this.User != null && this.User.IsInRole("Admin"))
            {
                forTheReciever = MessageType.FromAdmin;
            }

            if (sentMessage.Type == MessageType.System)
            {
                forTheReciever = MessageType.System;
            }

            MessageType forTheSender = MessageType.Sent;

            if (sentMessage.Type == MessageType.System)
            {
                forTheSender = MessageType.System;
            }

            var messageForTheReceiver = Message.ToMessage(sentMessage);

            messageForTheReceiver.Type = forTheReciever;

            var messageForTheSender = Message.ToMessage(sentMessage);

            messageForTheSender.Type = forTheSender;

            receiverUser.Messages.Add(messageForTheReceiver);
            senderUser.Messages.Add(messageForTheSender);

            receiverUser.Notifications.FromMessages += 1;
            senderUser.Notifications.FromMessages   += 1;

            db.SaveChanges();

            MessageManager.MessageHasBeenSent = true;

            return(RedirectToAction("OtherProfile", "Profile", new { @otherProfileId = receiverUser.Id }));
        }
        public ActionResult ExecuteTheTrade(int?favorId, string recieverId, string tradeOff)
        {
            if (User.Identity.GetUserId() == recieverId)
            {
                return(RedirectToAction("Details", "Favor", new { @id = favorId }));
            }

            if (!ParamsAreValid(favorId, recieverId, tradeOff))
            {
                return(RedirectToAction("Index", "Home"));
            }

            var db = new FavorDbContext();

            var senderId = User.Identity.GetUserId();

            if (db.FavorTradeModels.Any(f => f.SenderId == senderId && f.FavorId == favorId))
            {
                return(RedirectToAction("Details", "Favor", new { @id = favorId }));
            }

            var exchangeFavor = db.Favors.Find(favorId);

            var senderUser = db.Users
                             .FirstOrDefault(u => u.Id == senderId);

            var recieverUser = db.Users.Find(recieverId);

            if (!ParamsAreValid(exchangeFavor, recieverUser, senderUser))
            {
                return(RedirectToAction("Index", "Home"));
            }


            var favorTradeModel = new FavorTradeModel
            {
                FavorTitle       = exchangeFavor.Title,
                FavorContent     = exchangeFavor.Description,
                RecieverFullName = recieverUser.FullName,
                SenderFullName   = senderUser.FullName,
                SenderId         = senderUser.Id,
                RecieverId       = recieverUser.Id,
                TradeOff         = tradeOff,
                FavorId          = exchangeFavor.Id
            };

            recieverUser.PendingFavors.Add(favorTradeModel);
            senderUser.SentFavors.Add(favorTradeModel);

            recieverUser.Notifications.FromPendingFavors += 1;

            db.SaveChanges();

            return(RedirectToAction("Details", "Favor", new { @id = favorId }));
        }
        public ActionResult DeleteMessage(int id)
        {
            var db = new FavorDbContext();

            var messageToRemove = db.Messages.FirstOrDefault(m => m.Id == id);

            if (messageToRemove != null)
            {
                db.Messages.Remove(messageToRemove);

                db.SaveChanges();
            }

            return(RedirectToAction("Profile", "Profile"));
        }
Esempio n. 8
0
        public ActionResult Create(Data.Favor favor)
        {
            if (ModelState.IsValid)
            {
                var db = new FavorDbContext();

                var currentUserId = User.Identity.GetUserId();

                favor.UserId = currentUserId;

                var user = db.Users.Find(currentUserId);
                user.MyFavors.Add(favor);

                db.SaveChanges();
            }


            return(RedirectToAction("Index", "Home"));
        }
        public ActionResult DeleteTicket(int?id)
        {
            var db = new FavorDbContext();

            var currentUserEmail = User.Identity.GetUserName();
            var currentUser      = db.Users
                                   .Include(u => u.TicketsForAdmin)
                                   .Include(u => u.TicketsForSender)
                                   .FirstOrDefault(u => u.Email == currentUserEmail);

            if (this.User.IsInRole("Admin"))
            {
                currentUser.TicketsForAdmin.RemoveAll(t => t.Id == id);
            }

            else
            {
                currentUser.TicketsForSender.RemoveAll(t => t.Id == id);
            }

            db.SaveChanges();
            return(RedirectToAction("Profile", "Profile"));
        }
        public ActionResult CancelTrade(int?id)
        {
            if (!ParamsAreValid(id))
            {
                return(RedirectToAction("Profile", "Profile"));
            }

            var db = new FavorDbContext();

            var favorTradeModel = db.FavorTradeModels.Find(id);

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

            var userThatSendsCancelMessage = db.Users.FirstOrDefault(u => u.Id == favorTradeModel.SenderId);

            Message cancelMessage = new Message
            {
                ReceiverId  = favorTradeModel.RecieverId,
                SenderEmail = userThatSendsCancelMessage.Email,
                Title       = favorTradeModel.FavorTitle,
                Content     = "The trade favor with this title was canceled",
                Type        = MessageType.System
            };

            MessageController test = new MessageController();

            test.SendMessage(cancelMessage);

            db.FavorTradeModels.Remove(favorTradeModel);

            db.SaveChanges();

            return(RedirectToAction("Profile", "Profile"));
        }
        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"));
        }