public ActionResult Index()
        {
            using (CommonContext db = new CommonContext())
            {
                var subs = (from items in db.Subs
                            where items.UserID == User.Identity.Name
                            select items).ToList();

                List<Ad> model = new List<Ad>();
                foreach (var sub in subs)
                {
                    List<Ad> ad = (from items in db.Ads
                             where items.Id == sub.SubID
                             select items).ToList<Ad>();
                    if (ad.Count == 1)
                    {
                        Ad a = ad.Single<Ad>();
                        model.Add(a);    
                    }
                    
                }

                CustomUserInfo user = (from items in db.CustomUserInfos
                                       where items.UserName == User.Identity.Name
                                       select items).Single<CustomUserInfo>();

                ViewBag.User = user;
                ViewBag.IsUnread = IsUnread();
                model.Reverse();
                return View(model);
            }
        }
        public ActionResult Index()
        {
            List<Interlocutor> Interlocutors = new List<Interlocutor>();
            using (CommonContext ctx = new CommonContext())
            {
                List<MessageRel> mr = (from items in ctx.MessageRels 
                                           where items.Receiver == User.Identity.Name
                                       select items).ToList<MessageRel>();
                List<CustomUserInfo> users = new List<CustomUserInfo>();
                foreach (MessageRel item in mr)
                {
                    CustomUserInfo user = (from us in ctx.CustomUserInfos
                                           where us.UserName == item.Sender
                                           select us).Single<CustomUserInfo>();
                    if (user != null)
                        users.Add(user);
                }
                for (int i = 0; i < mr.Count; i++)
                {
                    Interlocutor inter = new Interlocutor();
                    inter.Unread = mr.ElementAt<MessageRel>(i).Unread;
                    inter.Name = users.ElementAt<CustomUserInfo>(i).Nickname;
                    inter.UserName = users.ElementAt<CustomUserInfo>(i).UserName;
                    inter.Image = users.ElementAt<CustomUserInfo>(i).Image;
                    Interlocutors.Add(inter);
                }
            }

            ViewBag.IsUnread = IsUnread();
            return View(Interlocutors);
        }
        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 Index()
 {
     using (CommonContext db = new CommonContext())
     {
         var model = (from items in db.Ads where items.OwnerID == User.Identity.Name select items).ToList();
         model.Reverse();
         ViewBag.IsUnread = IsUnread();
         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 Index(string Keywords, string Theme, string Location, string Popularity)
        {
            using (CommonContext db = new CommonContext())
            {
                List<Ad> sortedModel = new List<Ad>();
                List<Ad> model;
                if (Theme != "Усі теми")
                    model = (from items in db.Ads where items.Theme == Theme select items).ToList();
                else
                    model = (from items in db.Ads select items).ToList();
                if (Location != "Україна")
                    model = (from items in model where items.Country == Location select items).ToList();
                else
                    model = (from items in model select items).ToList();


                if (Keywords != "")
                {
                    if (model.Count != 0)
                    {
                        string[] keys = Keywords.Split(',');
                        for (int i = 0; i < keys.Length; i++)
                        {
                            string key = keys[i].Trim();
                            foreach (var m in model)
                            {
                                if (m.Keywords != null)
                                {
                                    if (m.Keywords.Contains(key))
                                    {
                                        sortedModel.Add(m);
                                    }
                                    
                                }
                            }
                        }
                    }
                }
                else
                    sortedModel = model;

                if (Popularity == "on")
                {
                    List<Ad> helper = (from item in sortedModel
                                       orderby item.Views descending
                                       select item).ToList<Ad>();
                    sortedModel = helper;
                }
                //sortedModel.Reverse();
                ViewBag.IsUnread = IsUnread();
                return View("~/Views/Ads/Index", sortedModel);
            }
        }
 public ActionResult Settings()
 {
     using (CommonContext ctx = new CommonContext())
     {
         var model = (from items in ctx.CustomUserInfos
                     where items.UserName == User.Identity.Name
                     select items).Single();
         ViewBag.First = model.FirstName;
         ViewBag.IsUnread = IsUnread();
         return View(model);
     }
 }
 public bool IsUnread()
 {
     bool IsUnread = false;
     using (CommonContext ctx = new CommonContext())
     {
         List<MessageRel> mr = (from items in ctx.MessageRels
                                where (items.Receiver == User.Identity.Name && items.Unread == true)
                                select items).ToList<MessageRel>();
         IsUnread = (mr.Count == 0) ? false : true;
     }
     return IsUnread;
 }
 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 Edit(int? id)
        {
            ViewBag.IsUnread = IsUnread();
            if (id == null)
            {
                return HttpNotFound();
            }
            using (CommonContext db = new CommonContext())
            {
                Ad advert = db.Ads.Find(id);
                if (advert != null)
                {
                    return View(advert);
                }

                return HttpNotFound();
            }
        }
        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);
            }
        }
Example #13
0
        public ActionResult Index(int page = 1)
        {
            int pageSize = 5; // количество объектов на страницу

            if (User.Identity.IsAuthenticated)
            {
                ViewBag.Us = User.Identity.IsAuthenticated;
                using (CommonContext db = new CommonContext())
                {
                    var model = db.Ads.ToList();
                    model.Reverse();
                    IEnumerable<Ad> adsPerPages = model.Skip((page - 1) * pageSize).Take(pageSize);
                    PageInfo pageInfo = new PageInfo { PageNumber = page, PageSize = pageSize, TotalItems = model.Count };
                    IndexViewModel ivm = new IndexViewModel { PageInfo = pageInfo, Ads = adsPerPages };
                    ViewBag.IsUnread = IsUnread();
                    return View(ivm);
                }   
            }

            return View("Guest");
            
        }
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<CommonContext>(null);

                try
                {
                    using (var context = new CommonContext())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
        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);
        }
Example #16
0
        public ActionResult Search(string Keywords, string Theme, string Location, string Popularity, int page = 1)
        {
            //this.TempData["Keywords"] = Keywords;
            ViewBag.Keywords = Keywords;
            ViewBag.Theme = Theme;
            //ViewBag.Location = Location;
            ViewBag.Popularity = Popularity;

            using (CommonContext db = new CommonContext())
            {
                List<Ad> sortedModel = new List<Ad>();
                List<Ad> model;
                if (Theme != "Усі теми")
                    model = (from items in db.Ads where items.Theme == Theme select items).ToList();
                else
                    model = (from items in db.Ads select items).ToList();
                if (Location != "Україна")
                    model = (from items in model where items.Country == Location select items).ToList();
                else
                    model = (from items in model select items).ToList();


                if (Keywords != "")
                {
                    if (model.Count != 0)
                    {
                        string[] keys = Keywords.Split(',');
                        for (int i = 0; i < keys.Length; i++)
                        {
                            string key = keys[i].Trim();
                            foreach (var m in model)
                            {
                                if (m.Keywords != null)
                                {
                                    if (m.Keywords.Contains(key))
                                    {
                                        sortedModel.Add(m);
                                    }

                                }
                            }
                        }
                    }
                }
                else
                    sortedModel = model;

                if (Popularity == "on")
                {
                    List<Ad> helper = (from item in sortedModel
                                       orderby item.Views descending
                                       select item).ToList<Ad>();
                    sortedModel = helper;
                }

                int pageSize = 2;
                IEnumerable<Ad> adsPerPages = model.Skip((page - 1) * pageSize).Take(pageSize);
                PageInfo pageInfo = new PageInfo { PageNumber = page, PageSize = pageSize, TotalItems = model.Count };
                IndexViewModel ivm = new IndexViewModel { PageInfo = pageInfo, Ads = sortedModel };

                //sortedModel.Reverse();
                ViewBag.IsUnread = IsUnread();
                return View(ivm);
            }
        }
Example #17
0
        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);
        }
        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");
        }
Example #19
0
        public ActionResult preSearch(string Keywords, int page = 1)
        {
            ViewBag.Keywords = Keywords;

            using (CommonContext db = new CommonContext())
            {
                List<Ad> sortedModel = new List<Ad>();
                List<Ad> model;
                model = (from items in db.Ads select items).ToList();

                if (Keywords != "")
                {
                    if (model.Count != 0)
                    {
                        string[] keys = Keywords.Split(',');
                        for (int i = 0; i < keys.Length; i++)
                        {
                            string key = keys[i].Trim();
                            foreach (var m in model)
                            {
                                if (m.Keywords != null)
                                {
                                    if (m.Keywords.Contains(key))
                                    {
                                        sortedModel.Add(m);
                                    }

                                }
                            }
                        }
                    }
                }
                else
                    sortedModel = model;
                
                int pageSize = 2;
                IEnumerable<Ad> adsPerPages = model.Skip((page - 1) * pageSize).Take(pageSize);
                PageInfo pageInfo = new PageInfo { PageNumber = page, PageSize = pageSize, TotalItems = model.Count };
                IndexViewModel ivm = new IndexViewModel { PageInfo = pageInfo, Ads = sortedModel };
                ViewBag.IsUnread = IsUnread();
                return View("Search", ivm);
            }
        }
        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);
        }
Example #21
0
        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 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);
        }