public IHttpActionResult PutPost(Guid id, Post post)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != post.Id)
            {
                return(BadRequest());
            }

            db.Entry(post).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PostExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Пример #2
0
        public IHttpActionResult PutProfile(Guid id, Profile profile)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != profile.Id)
            {
                return(BadRequest());
            }

            _profileService.UpdateProfile(profile);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProfileExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
 public void Create(User item)
 {
     //int id = this.GetAll().Max(u => u.Id);
     //item.Id = id;
     db.Users.Add(item);
     db.SaveChanges();
 }
        protected override void Seed(SocialNetworkContext context)
        {
            if (context.Roles.Any())
            {
                return;
            }

            var user = context.Users.First();

            var adminRole = new IdentityRole()
            {
                Name = "Admin"
            };

            var moderatorRole = new IdentityRole()
            {
                Name = "Moderator"
            };

            context.Roles.Add(adminRole);
            context.Roles.Add(moderatorRole);
            context.SaveChanges();

            adminRole.Users.Add(new IdentityUserRole()
            {
                UserId = user.Id
            });

            context.SaveChanges();
        }
Пример #5
0
        public ActionResult Create([Bind(Include = "ID,Name,Email")] User user)
        {
            try
            {
                User userFromDb = db.Users.Where(p => p.Email == user.Email).FirstOrDefault();

                //if (ModelState.IsValid)
                //{
                if (db.Users.Where(p => p.Email == user.Email).FirstOrDefault() == null)
                {
                    // if (ModelState.IsValid)
                    // {
                    db.Users.Add(user);
                    db.SaveChanges();
                    userFromDb = user;
                    //  }
                }
                // else
                //  return RedirectToAction("Index", "home");
                //}

                db.Asparaguses.Add(new Asparagus(userFromDb));
                db.SaveChanges();
                return(RedirectToAction("Index", "asparagus"));
            }
            catch
            {
                return(RedirectToAction("Index", "home"));
            }
        }
Пример #6
0
        public ActionResult Create([Bind(Include = "ID,Date")] Asparagus asparagus)
        {
            if (ModelState.IsValid)
            {
                db.Asparaguses.Add(asparagus);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(asparagus));
        }
Пример #7
0
        public ActionResult Create([Bind(Include = "Id,DateTimeSent,Content")] Message message)
        {
            if (ModelState.IsValid)
            {
                message.Id = Guid.NewGuid();
                db.Messages.Add(message);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(message));
        }
Пример #8
0
 public bool Remove(Profile profile)
 {
     try
     {
         _dbSet.Remove(profile);
         _socialNetworkContext.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Пример #9
0
 public bool Remove(Follow obj)
 {
     try
     {
         _dbSet.Remove(obj);
         _socialNetworkContext.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Пример #10
0
        public ActionResult Create([Bind(Include = "Id,Title,Description,CreationDate")] PhotoAlbum photoAlbum)
        {
            if (ModelState.IsValid)
            {
                photoAlbum.Id      = Guid.NewGuid();
                photoAlbum.Profile = db.Profiles.Find(Guid.Parse(Session["profileId"].ToString()));
                db.PhotoAlbums.Add(photoAlbum);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(photoAlbum));
        }
 public bool Remove(CommentsFromPost obj)
 {
     try
     {
         _dbSet.Remove(obj);
         _socialNetworkContext.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Пример #12
0
        private void AddTagsToAlbums(SocialNetworkContext db)
        {
            string[] sampleTags = new string[] { "#TestTag1", "#TestTag2", "#TestTag3", "#TestTag4", "#TestTag5" };

            foreach (var tag in sampleTags)
            {
                if (!db.Tags.Any(t => t.Title == tag))
                {
                    db.Tags.Add(new Tag
                    {
                        Title = tag
                    });
                }
                db.SaveChanges();
            }

            Random rnd = new Random();

            var albums   = db.Albums.Include(a => a.Tags).ToList();
            var dbTagIds = db.Tags.Select(t => t.Id).ToList();

            Console.WriteLine("Populating albums with tags...");

            foreach (var alb in albums)
            {
                int tagCount = rnd.Next(1, dbTagIds.Count);

                while (alb.Tags.Count < tagCount)
                {
                    var tagId = dbTagIds[rnd.Next(0, dbTagIds.Count)];

                    if (alb.Tags.Any(at => at.TagId == tagId))
                    {
                        continue;
                    }
                    else
                    {
                        alb.Tags.Add(new AlbumTag
                        {
                            TagId = tagId
                        });
                    }
                }
            }

            db.SaveChanges();

            Console.WriteLine("Done.");
        }
        public ActionResult Create([Bind(Include = "Id,Content")] Post post)
        {
            if (ModelState.IsValid)
            {
                post.Id     = Guid.NewGuid();
                post.Author = db.Profiles.ToList()
                              .Where(p => p.Email == Session["UserEmail"].ToString())
                              .First();
                db.Posts.Add(post);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(post));
        }
        public ActionResult Create([Bind(Include = "Id,Content")] Comment comment, Guid postId)
        {
            if (ModelState.IsValid)
            {
                comment.Id = Guid.NewGuid();
                var profileId = Guid.Parse(Session["profileId"].ToString());
                comment.Sender = db.Profiles.Find(profileId);
                comment.Post   = db.Posts.Find(postId);
                db.Comments.Add(comment);
                db.SaveChanges();
            }

            //return View(comment);
            return(RedirectToAction("Details", "Groups", new { id = Session["groupId"] }));
        }
Пример #15
0
 public IHttpActionResult CreatePost([FromBody] Post post)
 {
     try
     {
         using (var ctx = new SocialNetworkContext())
         {
             post.DateCreated = DateTime.Now;
             post.UserId      = GetCurrentUser().Id;
             ctx.Posts.Add(post);
             ctx.SaveChanges();
             return(Json(new
             {
                 success = true
             }));
         }
     }
     catch (Exception ex)
     {
         return(Json(new
         {
             success = false,
             error = ex.Message
         }));
     }
 }
Пример #16
0
        private static void RecieveTagsAndSaveThemToDb(SocialNetworkContext context)
        {
            Console.WriteLine("Enter tag:");
            string       tagAsString = Console.ReadLine();
            string       validTag    = tagAsString.Transform();
            List <Album> albums      = context.Albums.ToList();
            Random       rnd         = new Random();

            Tag tag = new Tag()
            {
                Name = validTag
            };

            for (int i = 0; i < 2; i++)
            {
                int next = rnd.Next(0, albums.Count);
                if (tag.Albums.All(a => a.AlbumId != next))
                {
                    tag.Albums.Add(new AlbumTags()
                    {
                        AlbumId = albums[next].Id
                    });
                }
            }
            context.Tags.Add(tag);

            context.SaveChanges();

            Console.WriteLine($"{tag} was added to database");
        }
        public void Create(Notification notification)
        {
            var notificationContext = new SocialNetworkContext();

            notificationContext.Notifications.Add(notification);
            notificationContext.SaveChanges();
        }
Пример #18
0
 public IHttpActionResult DeletePost(int id)
 {
     try
     {
         using (var ctx = new SocialNetworkContext())
         {
             var post = ctx.Posts.Where(w => w.Id == id).SingleOrDefault();
             if (post != null)
             {
                 ctx.Posts.Remove(post);
             }
             ctx.SaveChanges();
             return(Json(new
             {
                 success = true
             }));
         }
     }
     catch (Exception ex)
     {
         return(Json(new
         {
             success = false,
             error = ex.Message
         }));
     }
 }
Пример #19
0
        public IActionResult SignUp(User user)
        {
            Context.Entry(user).State = EntityState.Modified;
            Context.SaveChanges();

            return(View());
        }
Пример #20
0
        public static void LikeUserPost(int userId, int userPostId)
        {
            var context = new SocialNetworkContext();

            var user = context.Users.Find(userId);
            var post = context.Postings.Find(userPostId);
            var like = new Like()
            {
                Author  = user,
                Posting = post
            };

            context.Likes.Add(like);
            try
            {
                // Your code...
                // Could also be before try if you know the exception occurs in SaveChanges

                context.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
        }
Пример #21
0
 public void AddRange(IList <T> entities)
 {
     using (var context = new SocialNetworkContext())
     {
         context.Set <T>().AddRange(entities);
         context.SaveChanges();
     }
 }
        public ActionResult Create([Bind(Include = "Id,Content")] Message message, Guid recipientId)
        {
            if (ModelState.IsValid)
            {
                message.Id     = Guid.NewGuid();
                message.Sender = db.Users.ToList()
                                 .Where(user => user.Email == Session["userEmail"].ToString())
                                 .First();
                message.Recipient    = db.Users.Find(recipientId);
                message.DateTimeSent = DateTime.Now;
                db.Messages.Add(message);
                db.SaveChanges();
                return(RedirectToAction("Index", new { userId = recipientId }));
            }

            return(View("Index", new { userId = recipientId }));
        }
Пример #23
0
 public void DeleteEntity(T entity)
 {
     using (var context = new SocialNetworkContext())
     {
         context.Set <T>().Remove(entity);
         context.SaveChanges();
     }
 }
Пример #24
0
        public ActionResult Create([Bind(Include = "Id,Email,Name,Age,Photo")] Profile profile, HttpPostedFileBase profilePhoto)
        {
            if (ModelState.IsValid)
            {
                profile.Id    = Guid.NewGuid();
                profile.Email = Session["UserEmail"].ToString();
                //---- Upload da Foto ----
                profile.Photo = StorageService.BlobService.GetInstance()
                                .UploadFile("simplesocialnetwork", Guid.NewGuid().ToString(), profilePhoto.InputStream, profilePhoto.ContentType);
                //------------------------
                db.Profiles.Add(profile);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(profile));
        }
Пример #25
0
 public void AddEntity(T entity)
 {
     using (var context = new SocialNetworkContext())
     {
         context.Set <T>().Add(entity);
         context.SaveChanges();
     }
 }
Пример #26
0
        private static void CreateDb()
        {
            var ctx = new SocialNetworkContext();

            var users = ctx.Users.ToList();

            ctx.SaveChanges();
        }
        public ActionResult DeleteConfirmed(Guid id)
        {
            User user = db.Users.Find(id);

            db.Users.Remove(user);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Пример #28
0
 public bool Remove(int id)
 {
     try
     {
         var stories = new Story {
             Id = id
         };
         _context.Stories.Attach(stories);
         _context.Stories.Remove(stories);
         _context.SaveChanges();
         return(true);
     }
     catch (Exception ex)
     {
         throw;
     }
 }
        public ActionResult DeleteConfirmed(Guid id)
        {
            Profile profile = db.Profiles.Find(id);

            db.Profiles.Remove(profile);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Пример #30
0
 public bool UpdatePhoto(Guid idProfile, string urlPhoto)
 {
     try
     {
         var posts = _dbSet.Where(p => p.IdProfile == idProfile).ToList();
         for (var count = 0; count < posts.Count(); count++)
         {
             posts[count].UrlPhotoAutor = urlPhoto;
             _socialNetworkContext.Entry(posts[count]).State = EntityState.Modified;
         }
         _socialNetworkContext.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }