public ActionResult ConvoWith(string interlocutor)
        {
            interlocutor = (string)RouteData.Values["id"];
            List<UserMessage> messages;
            CustomUserInfo user; ;
            using(CommonContext ctx = new CommonContext())
            {
                messages = (from items in ctx.UsersMessages 
                                                  where ((items.Sender == interlocutor && 
                                                        items.Receiver == User.Identity.Name) ||
                                                        (items.Receiver == interlocutor &&
                                                        items.Sender == User.Identity.Name))
                                                    select items).ToList<UserMessage>();
            
                List<MessageRel> mrl = (from items in ctx.MessageRels 
                                     where (items.Receiver == User.Identity.Name && items.Sender == interlocutor)
                                 select items).ToList<MessageRel>();
                if (mrl.Count != 0)
                {
                    MessageRel mr = mrl.Single<MessageRel>();
                    mr.Unread = false;
                    ctx.Entry(mr).State = System.Data.EntityState.Modified;
                    ctx.SaveChanges();   
                }

                user = (from items in ctx.CustomUserInfos
                        where items.UserName == interlocutor
                        select items).Single<CustomUserInfo>();
            }
            messages.Reverse();
            ViewBag.Messages = messages;
            ViewBag.Interlocutor = user;
            ViewBag.IsUnread = IsUnread();
            return View();
        }
 public ActionResult Edit(Ad advert)
 {
     using (CommonContext db = new CommonContext())
     {
         Ad ad = db.Ads.Find(advert.Id);
         ad.Title = advert.Title;
         ad.Text = advert.Text;
         ad.Keywords = advert.Keywords;
         ad.Theme = advert.Theme;
         ad.Date = DateTime.Now;
         ad.Country = advert.Country;
         db.SaveChanges();
     }
     return RedirectToAction("Index");
 }
        public ActionResult New(Ad advert)
        {
            using (CommonContext db = new CommonContext()) 
            {
                advert.Date = DateTime.Now;
                advert.Engaged = 0;
                advert.Views = 0;
                advert.Theme = advert.Theme;
                advert.OwnerID = User.Identity.Name;
                advert.Country = advert.Country;

                db.Ads.Add(advert);
                db.SaveChanges();
            }            
            return RedirectToAction("Index");
        }
        public ActionResult RemoveAccount(string userName, FormCollection collection)
        {
            userName = User.Identity.Name;
            try
            {
                // TODO: Add delete logic here
                if (Roles.GetRolesForUser(userName).Count() > 0)
                {
                    Roles.RemoveUserFromRoles(userName, Roles.GetRolesForUser(userName));
                }
                ((SimpleMembershipProvider)Membership.Provider).DeleteAccount(userName); // deletes record from webpages_Membership table
                ((SimpleMembershipProvider)Membership.Provider).DeleteUser(userName, true); // deletes record from UserProfile table
                
                using (CommonContext ctx = new CommonContext())
                {
                    CustomUserInfo user = (from items in ctx.CustomUserInfos 
                                               where items.UserName == User.Identity.Name
                                               select items).Single<CustomUserInfo>();
                    ctx.Entry(user).State = System.Data.EntityState.Deleted;
                    ctx.SaveChanges();
                }
                
                foreach (var cookie in Request.Cookies.AllKeys)
                {
                    Request.Cookies.Remove(cookie);
                }
                foreach (var cookie in Response.Cookies.AllKeys)
                {
                    Response.Cookies.Remove(cookie);
                }

                // Invalidate the Cache on the Client Side
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Cache.SetNoStore();

                return RedirectToAction("Index", "Ads");
            }
            catch
            {
                return View(userName);
            }
        }
        public ActionResult ConvoWith()
        {
            string message = Request.Form["MessageText"];
            DateTime date = DateTime.Now;
            string sender = User.Identity.Name;
            string receiver = Request.Form["MessageReceiver"];

            using (CommonContext ctx = new CommonContext())
            {
                CustomUserInfo senderInfo = (from items in ctx.CustomUserInfos
                                             where items.UserName == sender
                                             select items).Single<CustomUserInfo>();
                CustomUserInfo receiverInfo = (from items in ctx.CustomUserInfos
                                               where items.UserName == receiver
                                             select items).Single<CustomUserInfo>();

                UserMessage msg = new UserMessage();
                msg.Sender = sender;
                msg.SenderName = senderInfo.Nickname;
                msg.Receiver = receiver;
                msg.ReceiverName = receiverInfo.Nickname;
                msg.Message = message;
                msg.Date = date;

                List<MessageRel> ml = (from items in ctx.MessageRels
                                       where items.Receiver == receiver
                                       select items).ToList<MessageRel>();
                MessageRel seekingMR = null;
                if (ml.Count != 0)
                {
                    foreach (MessageRel item in ml)
                    {
                        if (item.Sender == sender)
                        {
                            seekingMR = item;
                            seekingMR.Unread = true;
                            ctx.Entry(seekingMR).State = System.Data.EntityState.Modified;
                        }
                    }
                    if (seekingMR == null)
                    {
                        seekingMR = new MessageRel();
                        seekingMR.Receiver = receiver;
                        seekingMR.Sender = sender;
                        seekingMR.Unread = true;
                        ctx.Entry(seekingMR).State = System.Data.EntityState.Added;
                    }
                }
                else
                {
                    seekingMR = new MessageRel();
                    seekingMR.Receiver = receiver;
                    seekingMR.Sender = sender;
                    seekingMR.Unread = true;
                    ctx.Entry(seekingMR).State = System.Data.EntityState.Added;
                }

                ctx.Entry(msg).State = System.Data.EntityState.Added;
                ctx.SaveChanges();
            }

            return RedirectToAction("ConvoWith/" + receiver);
        }
        public ActionResult Settings(CustomUserInfo model, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                using (CommonContext ctx = new CommonContext())
                {

                    var user = (from items in ctx.CustomUserInfos
                                where
                                    items.UserName == User.Identity.Name
                                select items).Single();

                    if (file != null)
                    {
                        string[] fileName = (file.FileName).Split('.');
                        string ext = fileName[1];
                        string name = User.Identity.Name;
                        string uniqName = name + "." + ext;
                        string path = System.IO.Path.Combine(Server.MapPath("~/Content/Images/Users"), uniqName);

                        file.SaveAs(path);
                        user.Image = uniqName;
                    }
                    user.UserName = User.Identity.Name;
                    user.FirstName = model.FirstName;
                    user.LastName = model.LastName;
                    user.Nickname = model.Nickname;
                    user.Skills = model.Skills;
                    user.Country = model.Country;
                    user.Occupation = model.Occupation;
                    user.Interests = model.Interests;
                    user.Town = model.Town;
                    user.UserDescription = model.UserDescription;

                    ctx.Entry(user).State = System.Data.EntityState.Modified;
                    ctx.SaveChanges();
                }

            }
            return RedirectToAction("Index", "Ads");
        }
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (CommonContext db = new CommonContext())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);
                    using (CommonContext ctx = new CommonContext()){
                        CustomUserInfo user = new CustomUserInfo();
                        user.UserName = model.UserName;
                        user.FirstName = "Не вказано";
                        user.LastName = "Не вказано";
                        user.Nickname = "Не вказано";
                        user.Image = "default.png";
                        user.Country = "Не вказано";
                        user.Town = "Не вказано";
                        user.Occupation = "Не вказано";
                        user.Skills = "Не вказано";
                        user.UserDescription = "Не вказано";
                        user.LastTimeOnline = DateTime.Now;
                        ctx.Entry(user).State = System.Data.EntityState.Added;
                        ctx.SaveChanges();

                    }
                    return RedirectToAction("Index", "MySubs");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
 public ActionResult Delete(int? id)
 {
     using (CommonContext db = new CommonContext())
     {
         Ad ad = db.Ads.Find(id);
         db.Ads.Remove(ad);
         db.SaveChanges();
     }
     return RedirectToAction("Index");
 }
        public ActionResult No(int id)
        {
            ViewBag.IsUnread = IsUnread();
            AdView ad = new AdView();
            using (CommonContext db = new CommonContext())
            {
                var model = db.Ads.Find(id);
                model.Views++;
                db.SaveChanges();

                int subsID = Convert.ToInt32(id);

                List<Sub> subscribers;
                subscribers = (from items in db.Subs where items.SubID == subsID select items).ToList<Sub>();
                List<CustomUserInfo> users = new List<CustomUserInfo>();
                if (subscribers.Count != 0)
                    using (CommonContext ctx = new CommonContext())
                    {
                        foreach (var item in subscribers)
                        {
                            var user = (from items in ctx.CustomUserInfos where items.UserName == item.UserID select items).Single();
                            users.Add(user);
                        }
                    }
                ViewBag.Subscribers = users;

                ViewBag.Flag = false;

                foreach (var item in subscribers)
                    if (item.UserID == User.Identity.Name)
                        ViewBag.Flag = true;

                CustomUserInfo author = (from items in db.CustomUserInfos
                                         where items.UserName == model.OwnerID
                                         select items).Single<CustomUserInfo>();
                ad.Id = model.Id;
                ad.Title = model.Title;
                ad.Text = model.Text;
                ad.Date = model.Date;
                ad.Keywords = model.Keywords;
                ad.Theme = model.Theme;
                ad.Location = model.Country;
                ad.Image = model.Image;
                ad.Engaged = model.Engaged;
                ad.Views = model.Views;
                // author
                ad.UserName = author.UserName;
                ad.FirstName = author.FirstName;
                ad.LastName = author.LastName;
                ad.AuthorImage = author.Image;
                ad.Nickname = author.Nickname;
                ad.Occupation = author.Occupation;
                ad.LastTimeOnline = author.LastTimeOnline;

                return View("Ad", ad);
            }
        }
        public ActionResult Subscribe()
        {
            string UserID = Request.Form["UserID"];
            string path = "No/" + Request.Form["SubscriptionID"];
            int subsID = Convert.ToInt32(Request.Form["SubscriptionID"]);

            using (CommonContext db = new CommonContext())
            {
                Sub sub = new Sub();
                sub.SubID = subsID;
                sub.UserID = UserID;

                var entry = (from item in db.Subs
                             where
                                 (item.UserID == UserID &&
                                 item.SubID == subsID)
                             select item).ToList();
                if (entry.Count == 0)
                {
                    db.Subs.Add(sub);
                    db.SaveChanges();
                    using (CommonContext ctx = new CommonContext())
                    {
                        Ad ad = ctx.Ads.Find(subsID);
                        ad.Engaged++;
                        ctx.SaveChanges();
                    }
                }
            }
            return RedirectToAction(path);
        }