public async Task <bool> Follow([FromBody] ProfileViewModel vm)
        {
            try {
                var caller = await userManager.GetUserAsync(HttpContext.User);

                var trackUser = await userManager.FindByNameAsync(vm.UserName);

                if (trackUser != null)
                {
                    var follow = new Follow {
                        DateTime = DateTime.Now,
                        Owner    = caller,
                        Track    = trackUser
                    };
                    context.Followers.Add(follow);
                    UserStats.AddFollower(trackUser, context);
                    UserStats.AddFollowing(caller, context);
                    if (trackUser != caller)
                    {
                        Notification.Add(NotificationType.NewFollower, trackUser, caller, null, context);
                    }
                    await context.SaveChangesAsync();

                    return(true);
                }
            } catch (Exception ex) {
                ex = ex;
            }
            return(false);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("ID,Name,UserID,Title,Problem,Solution,Status,Team,UploadDate")] Idea idea)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user.Firstname != null)
            {
                idea.Name = user.Firstname + " " + user.Lastname;
            }
            else
            {
                idea.Name = "Anon";
            }
            idea.UserID     = user.Id;
            idea.Status     = "Pending";
            idea.Team       = user.Team;
            idea.UploadDate = DateTime.Now;
            if (ModelState.IsValid)
            {
                _context.Add(idea);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(idea));
        }
Exemplo n.º 3
0
        public static async Task UpdateUpdate(ListViewModel vm, ApplicationUser owner, IdeasContext context, IHostingEnvironment hostingEnvironment)
        {
            var update = GetByUpdateFromIdea(vm.IdeaId, vm.EditId, owner, context).Result;

            if (update != null)
            {
                var jsonRemove = ((vm.EditUpdateViewModel.JsonImagesRemove != null) ? JsonConvert.DeserializeObject <List <Image> >(vm.EditUpdateViewModel.JsonImagesRemove) : null);
                var jsonNew    = ((vm.EditUpdateViewModel.JsonImagesNew != null) ? JsonConvert.DeserializeObject <List <Image> >(vm.EditUpdateViewModel.JsonImagesNew) : null);
                var AddThis    = cleanUnSavedImage(jsonNew, jsonRemove, owner, context, hostingEnvironment).Result;
                finishRemoveList(vm.IdeaId, vm.EditId, jsonRemove, context, hostingEnvironment);
                try {
                    update      = context.Updates.First(r => r.Id == vm.EditId);
                    update.Text = vm.EditUpdateViewModel.Text.Trim();
                    if (AddThis != null)
                    {
                        AddThis = UnsavedImage.GetList(AddThis.Select(r => r.FileName).ToList(), context, owner);
                        if (update.Images != null)
                        {
                            foreach (Image image in AddThis)
                            {
                                update.Images.Add(image);
                            }
                        }
                        else
                        {
                            update.Images = AddThis;
                        }
                        UnsavedImage.RemoveList(AddThis.Select(r => r.FileName).ToList(), context, owner);
                    }
                    await context.SaveChangesAsync();
                } catch (Exception ex) {
                    ex = ex;
                }
            }
        }
Exemplo n.º 4
0
        public static async Task Remove(ApplicationUser owner, ListViewModel vm, IdeasContext context, IHostingEnvironment hostingEnviroment)
        {
            try {
                var update = await context.Ideas
                             .Where(r => r.Id == vm.IdeaId && r.Owner == owner)
                             .Select(o => o.Updates
                                     .Where(g => g.Id == vm.EditId).Select(x => new Update {
                    Id       = x.Id,
                    Text     = x.Text,
                    DateTime = x.DateTime,
                    Images   = x.Images.Select(z => new Image {
                        Id = z.Id, Name = z.Name, Height = z.Height, Width = z.Width, FileName = z.FileName, OriginName = z.OriginName
                    }).ToList()
                }).FirstOrDefault()).FirstOrDefaultAsync();

                if (update != null)
                {
                    foreach (Image image in update.Images)
                    {
                        await Image.DeleteFromServer(image.FileName, context, hostingEnviroment);
                    }
                    context.Images.RemoveRange(update.Images);
                    context.Updates.Remove(update);
                    await context.SaveChangesAsync();
                }
            } catch (Exception ex) {
                ex = ex;
            }
        }
        public async static void Remove(int id, IdeasContext context, ApplicationUser owner)
        {
            var image = context.UnsavedImages.Where(r => r.Id == id && r.Owner == owner).FirstOrDefault();

            context.UnsavedImages.Remove(image);
            await context.SaveChangesAsync();
        }
Exemplo n.º 6
0
        public static async Task PostUpdate(ListViewModel vm, ApplicationUser owner, IdeasContext context, IHostingEnvironment hostingEnvironment)
        {
            var idea = await context.Ideas.Include(r => r.Updates).FirstOrDefaultAsync(r => r.Id == vm.IdeaId && r.Owner == owner);

            if (idea != null)
            {
                var AddThis = Image.GetNewAndRemoveOld(vm.EditUpdateViewModel.JsonImagesNew, vm.EditUpdateViewModel.JsonImagesRemove, vm.IdeaId, vm.EditId, owner, hostingEnvironment, context).Result;
                try {
                    var update = new Update();
                    update.Text     = vm.EditUpdateViewModel.Text.Trim();
                    update.DateTime = DateTime.Now;
                    if (AddThis != null)
                    {
                        AddThis       = UnsavedImage.GetList(AddThis.Select(r => r.FileName).ToList(), context, owner);
                        update.Images = AddThis;
                        UnsavedImage.RemoveList(AddThis.Select(r => r.FileName).ToList(), context, owner);
                    }
                    idea.Updates.Add(update);
                    idea.LatestUpdate = DateTime.Now;
                    Stats.AddUpdate(idea);
                    await context.SaveChangesAsync();
                } catch (Exception ex) {
                    ex = ex;
                }
            }
        }
Exemplo n.º 7
0
        public async Task <IActionResult> ClearNotifications()
        {
            var notifications = from i in _context.Notifcation select i;
            var user          = await _userManager.GetUserAsync(User);

            foreach (var notification in notifications)
            {
                if (notification.TargetUserID != null && notification.TargetUserID.Equals(user.Id))
                {
                    _context.Notifcation.Remove(notification);
                }
            }
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Notifications)));
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Create([Bind("Text")] Idea idea)
        {
            User user = (User)RouteData.Values["User"];

            idea.UserId = user.Id;
            if (idea.Text != null && idea.Text.Length > 0)
            {
                _context.Add(idea);
                await _context.SaveChangesAsync();

                TempData["Success"] = true;
                return(RedirectToAction(nameof(MyIdeas)));
            }
            ModelState.AddModelError(string.Empty, "Specify your idea.");
            return(View(idea));
        }
        public async Task <IActionResult> ViewedNotifications()
        {
            var owner = await userManager.GetUserAsync(HttpContext.User);

            Notification.SetViewed(owner, context);
            await context.SaveChangesAsync();

            return(Json(""));
        }
Exemplo n.º 10
0
        public static async Task <int> Remove(ApplicationUser owner, ListViewModel vm, IdeasContext context, IHostingEnvironment hostingEnvironment)
        {
            var amountoffavs = 0;
            var idea         = await context.Ideas
                               .Where(r => r.Id == vm.IdeaId && r.Owner == owner)
                               .Select(pr => new Idea {
                Id           = pr.Id,
                Titel        = pr.Titel,
                Text         = pr.Text,
                Stats        = pr.Stats,
                DateTime     = pr.DateTime,
                LatestUpdate = pr.LatestUpdate,
                Favorites    = pr.Favorites,
                Updates      = pr.Updates.Select(z => new Update {
                    Id = z.Id, DateTime = z.DateTime, Text = z.Text, Images = z.Images
                }).ToList(),
                Comments = pr.Comments,
                Images   = pr.Images,
                Tags     = pr.Tags
            }
                                       ).FirstOrDefaultAsync();

            if (idea != null)
            {
                try {
                    amountoffavs = idea.Favorites.Count;

                    List <Image> listofimages = new List <Image>();
                    listofimages.AddRange(idea.Images);

                    foreach (Models.Database.Update update in idea.Updates)
                    {
                        listofimages.AddRange(update.Images);
                    }

                    foreach (Image image in listofimages)
                    {
                        await Image.DeleteFromServer(image.FileName, context, hostingEnvironment);
                    }
                    context.Images.RemoveRange(listofimages);

                    if (idea.Stats != null)
                    {
                        context.Stats.Remove(idea.Stats);
                    }
                    context.Tags.RemoveRange(idea.Tags);
                    context.Comments.RemoveRange(idea.Comments);
                    context.Updates.RemoveRange(idea.Updates);
                    context.Favorites.RemoveRange(idea.Favorites);
                    context.Ideas.Remove(idea);
                    await context.SaveChangesAsync();
                } catch (Exception ex) {
                    ex = ex;
                }
            }
            return(amountoffavs);
        }
Exemplo n.º 11
0
        public static async Task UpdateIdea(ListViewModel vm, ApplicationUser owner, IdeasContext context, IHostingEnvironment hostingEnvironment)
        {
            var idea = context.Ideas.Include(r => r.Tags).FirstOrDefault(r => r.Id == vm.IdeaId && r.Owner == owner);

            if (idea != null)
            {
                var jsonRemove = ((vm.EditIdeaViewModel.JsonImagesRemove != null) ? JsonConvert.DeserializeObject <List <Image> >(vm.EditIdeaViewModel.JsonImagesRemove) : null);
                var jsonNew    = ((vm.EditIdeaViewModel.JsonImagesNew != null) ? JsonConvert.DeserializeObject <List <Image> >(vm.EditIdeaViewModel.JsonImagesNew) : null);
                var AddThis    = cleanUnSavedImage(jsonNew, jsonRemove, owner, context, hostingEnvironment).Result;
                finishRemoveList(vm.IdeaId, jsonRemove, context, hostingEnvironment);

                try {
                    var jsonTagsRemove = ((vm.EditIdeaViewModel.JsonTagsRemove != null) ? JsonConvert.DeserializeObject <List <Tag> >(vm.EditIdeaViewModel.JsonTagsRemove) : null);
                    var jsonTagsNew    = ((vm.EditIdeaViewModel.JsonTags != null) ? JsonConvert.DeserializeObject <List <Tag> >(vm.EditIdeaViewModel.JsonTags) : null);

                    idea.Text  = vm.EditIdeaViewModel.Text.Trim();
                    idea.Titel = vm.EditIdeaViewModel.Titel.Trim();
                    if (jsonTagsRemove != null)
                    {
                        var remove = context.Tags.Where(r => jsonTagsRemove.Any(x => x.Value == r.Value)).ToList();
                        idea.Tags.RemoveAll(r => remove.Any(s => s.Value == r.Value));
                    }
                    if (jsonTagsNew != null)
                    {
                        if (idea.Tags != null && idea.Tags.Count > 0)
                        {
                            jsonTagsNew = jsonTagsNew.Where(r => !idea.Tags.Any(a => r.Value == a.Value)).ToList();
                            idea.Tags.AddRange(jsonTagsNew);
                        }
                        else
                        {
                            idea.Tags = jsonTagsNew;
                        }
                    }

                    if (AddThis != null)
                    {
                        AddThis = UnsavedImage.GetList(AddThis.Select(r => r.FileName).ToList(), context, owner);
                        if (idea.Images != null)
                        {
                            foreach (Image image in AddThis)
                            {
                                idea.Images.Add(image);
                            }
                        }
                        else
                        {
                            idea.Images = AddThis;
                        }
                        UnsavedImage.RemoveList(AddThis.Select(r => r.FileName).ToList(), context, owner);
                    }
                    await context.SaveChangesAsync();
                } catch (Exception ex) {
                    ex = ex;
                }
            }
        }
Exemplo n.º 12
0
 public static async void Post(Tag tag, IdeasContext context)
 {
     try {
         tag.DateTime = DateTime.Now;
         context.Tags.Add(tag);
         await context.SaveChangesAsync();
     } catch (Exception ex) {
         ex = ex;
     }
 }
        public async static void RemoveList(List <string> filenames, IdeasContext context, ApplicationUser owner)
        {
            var list = context.UnsavedImages.Where(r => filenames.Any(x => x == r.FileName) && r.Owner == owner).ToList();

            foreach (UnsavedImage unsaved in list)
            {
                context.UnsavedImages.Remove(unsaved);
            }
            await context.SaveChangesAsync();
        }
 public async static void SaveList(List <Image> unsavedImage, ApplicationUser owner, IdeasContext context)
 {
     foreach (Image image in unsavedImage)
     {
         var unsaved = new UnsavedImage(image);
         unsaved.Owner = owner;
         context.UnsavedImages.Add(unsaved);
     }
     await context.SaveChangesAsync();
 }
Exemplo n.º 15
0
        public async Task <IActionResult> CreateComment([Bind("CommentID,IdeaID,UserID,UserComment")] Comment comment)
        {
            var user = await _userManager.FindByIdAsync(comment.UserID);

            var  subscriptions = from i in _context.Subscribe select i;
            var  idea          = from i in _context.Idea select i;
            Idea userIdea;

            userIdea = await idea.Where(i => i.ID.Equals(comment.IdeaID)).FirstOrDefaultAsync();

            subscriptions    = subscriptions.Where(i => i.IdeaID.Equals(comment.IdeaID) && i.Subscribed == true);
            comment.UserID   = user.Id;
            comment.UserName = user.Firstname + " " + user.Lastname;
            string message = user.Firstname + " " + user.Lastname + " has commented on idea: " + _context.Idea.Where(i => i.ID == comment.IdeaID).FirstOrDefault().Title;

            if (ModelState.IsValid)
            {
                _context.Add(comment);
                await _context.SaveChangesAsync();

                foreach (var subscription in subscriptions)
                {
                    Notification newNotification = new Notification
                    {
                        IdeaID              = comment.IdeaID,
                        UserID              = comment.UserID,
                        TargetUserID        = subscription.UserID,
                        Username            = userIdea.Name,
                        NotificationMessage = message,
                        Checked             = false,
                        NotificationDate    = System.DateTime.Now
                    };
                    _context.Notifcation.Add(newNotification);
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(IdeasController.Index), "Ideas"));
            }
            return(RedirectToAction(nameof(HomeController.Error), "Error"));
        }
Exemplo n.º 16
0
 public static async void PostList(List <Tag> tags, IdeasContext context)
 {
     try {
         // context.Tags.Where(t => tags.Any(x => x.Value == t.Value) && idea.Id == t.idea )).ToList();
         foreach (Tag tag in tags)
         {
             tag.DateTime = DateTime.Now;
             context.Tags.Add(tag);
         }
         await context.SaveChangesAsync();
     } catch (Exception ex) {
         ex = ex;
     }
 }
Exemplo n.º 17
0
        public async Task <IActionResult> Edit(EditViewModel vm)
        {
            if (ModelState.IsValid)
            {
                var emailcheck = await _userManager.FindByEmailAsync(vm.ApplicationUser.Email);

                if (emailcheck == null || (emailcheck.Email == vm.ApplicationUser.Email))
                {
                    var owner = await _userManager.GetUserAsync(HttpContext.User);

                    var user = context.ApplicationUsers.Include(r => r.Image).Where(r => r.Id == owner.Id).FirstOrDefault();
                    if (vm.JsonImages != null)
                    {
                        var image = JsonConvert.DeserializeObject <List <Image> >(vm.JsonImages)[0];
                        image      = context.Images.Where(r => r.Id == image.Id).FirstOrDefault();
                        user.Image = image;
                    }
                    user.Email = vm.ApplicationUser.Email;
                    if (vm.ApplicationUser.UserName.Length > 3)
                    {
                        user.UserName = vm.ApplicationUser.UserName;
                    }
                    user.Description = vm.ApplicationUser.Description;
                    await context.SaveChangesAsync();

                    vm.ApplicationUser = user;
                }
                else
                {
                    var error = new IdentityError {
                        Description = "E-mail address is already in use."
                    };
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }
            return(View(vm));
        }
Exemplo n.º 18
0
 private static async Task <List <Image> > cleanUnSavedImage(List <Image> jsonNew, List <Image> jsonRemove, ApplicationUser owner, IdeasContext context, IHostingEnvironment hostingEnvironment)
 {
     if (jsonNew != null && jsonRemove != null)
     {
         var notsaved = jsonNew.Where(r => jsonRemove.Any(x => x.FileName == r.FileName)).ToList();
         var unsaved  = context.UnsavedImages.Where(r => notsaved.Any(x => x.FileName == r.FileName) && r.Owner == owner).ToList();
         foreach (UnsavedImage image in unsaved)
         {
             await Image.DeleteFromServer(image.FileName, context, hostingEnvironment);
         }
         UnsavedImage.RemoveList(unsaved.Select(r => r.FileName).ToList(), context, owner);
         jsonNew.RemoveAll(r => unsaved.Any(x => r.FileName == x.FileName));
         await context.SaveChangesAsync();
     }
     return(jsonNew);
 }
        public async Task <IActionResult> Create(DetailsViewModel model, string returnUrl)
        {
            Comment comment = model.NewComment;
            User    user    = (User)RouteData.Values["User"];

            comment.UserId = user.Id;

            if (comment.Text != null && comment.Text.Length > 0)
            {
                _context.Add(comment);
                await _context.SaveChangesAsync();

                return(Redirect(returnUrl));
            }
            return(Redirect(returnUrl));
        }
Exemplo n.º 20
0
 public static async void RemoveList(List <Tag> tags, IdeasContext context)
 {
     context.Tags.RemoveRange(tags);
     await context.SaveChangesAsync();
 }
 public async static void Save(UnsavedImage unsavedImage, IdeasContext context)
 {
     context.UnsavedImages.Add(unsavedImage);
     await context.SaveChangesAsync();
 }
Exemplo n.º 22
0
        public async Task <IActionResult> SubscribeToIdea([Bind("SubscriptionID,UserID,Subscribed,IdeaID")] Subscribe subscribe)
        {
            var user = await _userManager.GetUserAsync(User);

            var  subscriptions = from i in _context.Subscribe select i;
            var  idea          = from i in _context.Idea select i;
            Idea userIdea;

            userIdea = await idea.Where(i => i.ID.Equals(subscribe.IdeaID)).FirstOrDefaultAsync();

            subscriptions    = subscriptions.Where(i => i.IdeaID.Equals(subscribe.IdeaID) && i.Subscribed == true);
            subscribe.UserID = user.Id;
            string message = "";

            if (subscribe.Subscribed == true)
            {
                subscribe.Subscribed = false;
                message = user.Firstname + " " + user.Lastname + " has Unsubscribed to the idea: " + _context.Idea.Where(i => i.ID == subscribe.IdeaID).FirstOrDefault().Title;
            }
            else if (subscribe.Subscribed == false)
            {
                subscribe.Subscribed = true;
                message = user.Firstname + " " + user.Lastname + " has Subscribed to the idea: " + _context.Idea.Where(i => i.ID == subscribe.IdeaID).FirstOrDefault().Title;
            }
            if (ModelState.IsValid)
            {
                if (!_context.Subscribe.Any(e => e.UserID.Equals(user.Id) && e.IdeaID == subscribe.IdeaID))
                {
                    _context.Add(subscribe);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    try
                    {
                        _context.Subscribe.Update(subscribe);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!SubscribeExists(subscribe.SubscriptionID))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                foreach (var subscription in subscriptions)
                {
                    Notification newNotification = new Notification
                    {
                        IdeaID              = subscribe.IdeaID,
                        UserID              = subscribe.UserID,
                        TargetUserID        = subscription.UserID,
                        Username            = userIdea.Name,
                        NotificationMessage = message,
                        Checked             = false,
                        NotificationDate    = System.DateTime.Now
                    };
                    _context.Notifcation.Add(newNotification);
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(IdeasController.Index), "Ideas"));
            }
            return(RedirectToAction(nameof(HomeController.Error), "Error"));
        }