public async Task <IActionResult> Create([Bind("Id,UserId,CommunityId,DateOfJoining,Status")] CommunityMember communityMember)
        {
            if (ModelState.IsValid)
            {
                _context.Add(communityMember);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", "Communities"));
            }
            ViewData["CommunityId"] = new SelectList(_context.Community, "Id", "CommunityName", communityMember.CommunityId);
            ViewData["UserId"]      = new SelectList(_context.User, "Id", "Email", communityMember.UserId);
            return(View("Index", "Community"));
        }
        public async Task <IActionResult> Create([Bind("Id,UserEmail,Message,PhotoUrl,PublishDateTime")] Post post)
        {
            if (ModelState.IsValid)
            {
                post.UserEmail       = User.Identity.Name;
                post.PublishDateTime = DateTime.Now;
                post.Id = Guid.NewGuid();
                _context.Add(post);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(post));
        }
        public async Task <IActionResult> CreateProfile([FromForm] CreateProfileDto createProfileDto, [FromQuery] bool edit)
        {
            Domain.Profile profile = null;

            if (edit)
            {
                profile = await _context.Profiles.Include(p => p.Skills).FirstOrDefaultAsync(p => p.UserId == UserId);

                if (profile.DisplayName != createProfileDto.DisplayName &&
                    _context.Profiles.Any(p => p.DisplayName == createProfileDto.DisplayName))
                {
                    return(BadRequest());
                }

                _context.Skills.RemoveRange(profile.Skills);

                _mapper.Map <CreateProfileDto, Domain.Profile>(createProfileDto, profile);

                if (createProfileDto.Avatar != null)
                {
                    var avatar = UploadImage(createProfileDto.Avatar);
                    profile.Avatar = avatar;
                }
            }
            else
            {
                if (createProfileDto.Avatar == null)
                {
                    return(BadRequest());
                }

                if (_context.Profiles.Any(p => p.DisplayName == createProfileDto.DisplayName))
                {
                    return(BadRequest());
                }

                profile        = _mapper.Map <Domain.Profile>(createProfileDto);
                profile.UserId = UserId;
                var avatar = UploadImage(createProfileDto.Avatar);
                profile.Avatar = avatar;

                _context.Profiles.Add(profile);
            }

            await _context.SaveChangesAsync();

            var profileDto = _mapper.Map <ProfileDto>(profile);

            return(Ok(profileDto));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> PutPerson([FromRoute] int id, [FromBody] Person person)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != person.ID)
            {
                return(BadRequest());
            }

            _context.Entry(person).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PersonExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,Phone,Gender,Age,Email,Password,UserType,DateOfBirth,ProfileImage,SecurityQuestionAnswer,SecurityQuestionID,CityID,CountryID")] User user)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = UploadedFile(user);
                user.ProfilePicture = uniqueFileName;
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CityID"]             = new SelectList(_context.City, "Id", "CityName", user.CityID);
            ViewData["CountryID"]          = new SelectList(_context.Country, "Id", "CountryName", user.CountryID);
            ViewData["SecurityQuestionID"] = new SelectList(_context.SecurityQuestion, "Id", "Question", user.SecurityQuestionID);
            return(View(user));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Create([Bind("Id,RelatingUserId,RelatedUserId,RequestStatus,RelationshipStatus,DateOfRequest,DateOfAcceptance")] UserRelationship userRelationship)
        {
            if (ModelState.IsValid)
            {
                _context.Add(userRelationship);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["RelatedUserId"]  = new SelectList(_context.User, "Id", "Email", userRelationship.RelatedUserId);
            ViewData["RelatingUserId"] = new SelectList(_context.User, "Id", "Email", userRelationship.RelatingUserId);
            return(View(userRelationship));
        }
Ejemplo n.º 7
0
 public async Task <int> UpdateCountsAsync(int id, int memberCount, int storyCount)
 {
     try
     {
         Group group = new Group();
         group.Id          = id;
         group.MemberCount = memberCount;
         group.StoryCount  = storyCount;
         _context.Groups.Attach(group);
         var entry = _context.Entry(group);
         entry.Property(e => e.MemberCount).IsModified = true;
         entry.Property(e => e.StoryCount).IsModified  = true;
         return(await _context.SaveChangesAsync());
     }
     catch (Exception ex)
     {
         throw;
     }
 }
Ejemplo n.º 8
0
        public async Task <IActionResult> Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                UserModel user = await db.Users.FirstOrDefaultAsync(u => u.Login == model.Login);

                if (user == null)
                {
                    db.Users.Add(new UserModel {
                        Login = model.Login, Name = model.Name, Password = model.Password, Sex = model.Sex
                    });
                    await db.SaveChangesAsync();
                    await Authenticate(model.Login);

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "Некорректные логин и(или) пароль");
                }
            }
            return(View(model));
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> Register([FromBody] RegisterDto registerDto)
        {
            if (await _context.Users.Where(x => x.Email == registerDto.Email).AnyAsync())
            {
                return(BadRequest(new Error("User already exits")));
            }

            byte[] passwordHash, passwordSalt;
            PasswordUtil.CreatePasswordHash(registerDto.Password, out passwordHash, out passwordSalt);

            var newUser = new User();

            newUser.Email        = registerDto.Email;
            newUser.PasswordHash = passwordHash;
            newUser.PasswordSalt = passwordSalt;

            _context.Users.Add(newUser);
            await _context.SaveChangesAsync();

            var tokenString = GetToken(newUser);

            return(Ok(new { token = tokenString }));
        }
Ejemplo n.º 10
0
 public async Task <int> SaveChangesAsync()
 {
     return(await context.SaveChangesAsync());
 }
Ejemplo n.º 11
0
        public static async Task GenerateTestData(SocialNetworkContext socialNetworkContext)
        {
            var appUser = new ApplicationUser();

            appUser.UserName     = "******";
            appUser.Email        = "*****@*****.**";
            appUser.PasswordHash = "AGwu0Rve83bj8IDQ6TYxjEguZBtQfOCyOmJZfUAAg/lYiLCQasQPE9yp+pi/cIl5+w==";

            var appUser2 = new ApplicationUser();

            appUser2.UserName     = "******";
            appUser2.Email        = "*****@*****.**";
            appUser2.PasswordHash = "AGwu0Rve83bj8IDQ6TYxjEguZBtQfOCyOmJZfUAAg/lYiLCQasQPE9yp+pi/cIl5+w==";


            var appUser3 = new ApplicationUser();

            appUser3.UserName     = "******";
            appUser3.Email        = "*****@*****.**";
            appUser3.PasswordHash = "AGwu0Rve83bj8IDQ6TYxjEguZBtQfOCyOmJZfUAAg/lYiLCQasQPE9yp+pi/cIl5+w==";

            var appUser4 = new ApplicationUser();

            appUser4.UserName     = "******";
            appUser4.Email        = "*****@*****.**";
            appUser4.PasswordHash = "AGwu0Rve83bj8IDQ6TYxjEguZBtQfOCyOmJZfUAAg/lYiLCQasQPE9yp+pi/cIl5+w==";


            var appUser5 = new ApplicationUser();

            appUser5.UserName     = "******";
            appUser5.Email        = "*****@*****.**";
            appUser5.PasswordHash = "AGwu0Rve83bj8IDQ6TYxjEguZBtQfOCyOmJZfUAAg/lYiLCQasQPE9yp+pi/cIl5+w==";

            var appUser6 = new ApplicationUser();

            appUser6.UserName     = "******";
            appUser6.Email        = "*****@*****.**";
            appUser6.PasswordHash = "AGwu0Rve83bj8IDQ6TYxjEguZBtQfOCyOmJZfUAAg/lYiLCQasQPE9yp+pi/cIl5+w==";

            var appUser7 = new ApplicationUser();

            appUser7.UserName     = "******";
            appUser7.Email        = "*****@*****.**";
            appUser7.PasswordHash = "AGwu0Rve83bj8IDQ6TYxjEguZBtQfOCyOmJZfUAAg/lYiLCQasQPE9yp+pi/cIl5+w==";

            var user = new User
            {
                ApplicationUser = appUser,
                Posts           = new List <Post>
                {
                    new Post
                    {
                        Content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean purus urna, egestas nec aliquam in, tempus quis diam. Sed non odio a augue tempor ornare."
                    }
                },
                ProfilePictureUrl = "https://via.placeholder.com/150"
            };

            var user2 = new User
            {
                ApplicationUser = appUser2,
                Posts           = new List <Post>
                {
                    new Post
                    {
                        Content  = "Praesent luctus interdum diam placerat condimentum. Sed rutrum dapibus dui quis mattis. Donec id porta odio. Duis non consequat lorem.",
                        Comments = new List <Comment>()
                        {
                            new Comment
                            {
                                Content = "Very cool!",
                                User    = user
                            }
                        }
                    }
                },
                ProfilePictureUrl = "https://via.placeholder.com/150"
            };

            var friendship = new Friendship
            {
                User1 = user,
                User2 = user2
            };


            var user3 = new User
            {
                ApplicationUser = appUser3,
                Posts           = new List <Post>
                {
                    new Post
                    {
                        Content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    }
                },
                ProfilePictureUrl = "https://via.placeholder.com/150"
            };

            var user4 = new User
            {
                ApplicationUser = appUser4,
                Posts           = new List <Post>
                {
                    new Post
                    {
                        Content  = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
                        Comments = new List <Comment>()
                        {
                            new Comment
                            {
                                Content = "Not so cool!",
                                User    = user3
                            }
                        }
                    }
                },
                ProfilePictureUrl = "https://via.placeholder.com/150"
            };

            var friendship2 = new Friendship
            {
                User1 = user3,
                User2 = user4
            };


            var user6 = new User
            {
                ApplicationUser = appUser6,
                Posts           = new List <Post>
                {
                    new Post
                    {
                        Content = "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                    }
                },
                ProfilePictureUrl = "https://via.placeholder.com/150"
            };

            var user5 = new User
            {
                ApplicationUser = appUser5,
                Posts           = new List <Post>
                {
                    new Post
                    {
                        Content  = "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
                        Comments = new List <Comment>()
                        {
                            new Comment
                            {
                                Content = "Kind of cool!",
                                User    = user6
                            }
                        }
                    }
                },
                ProfilePictureUrl = "https://via.placeholder.com/150"
            };


            var user7 = new User
            {
                ApplicationUser = appUser7,
                Posts           = new List <Post>
                {
                    new Post
                    {
                        Content  = " Etiam sit amet nisl purus in. Erat imperdiet sed euismod nisi porta.",
                        Comments = new List <Comment>()
                        {
                            new Comment
                            {
                                Content = "Kind of cool!",
                                User    = user6
                            },
                            new Comment
                            {
                                Content = "It's ok",
                                User    = user5
                            }
                        }
                    }
                },
                ProfilePictureUrl = "https://via.placeholder.com/150"
            };

            var friendship3 = new Friendship
            {
                User1 = user5,
                User2 = user6
            };

            var friendship4 = new Friendship
            {
                User1 = user5,
                User2 = user7
            };

            var friendship5 = new Friendship
            {
                User1 = user6,
                User2 = user7
            };

            socialNetworkContext.SocialNetworkUsers.Add(user);
            socialNetworkContext.SocialNetworkUsers.Add(user2);
            socialNetworkContext.SocialNetworkUsers.Add(user3);
            socialNetworkContext.SocialNetworkUsers.Add(user4);
            socialNetworkContext.SocialNetworkUsers.Add(user5);
            socialNetworkContext.SocialNetworkUsers.Add(user6);
            socialNetworkContext.SocialNetworkUsers.Add(user7);
            socialNetworkContext.Friendships.Add(friendship);
            socialNetworkContext.Friendships.Add(friendship2);
            socialNetworkContext.Friendships.Add(friendship3);
            socialNetworkContext.Friendships.Add(friendship4);
            socialNetworkContext.Friendships.Add(friendship5);
            await socialNetworkContext.SaveChangesAsync();
        }
Ejemplo n.º 12
0
        public async Task <IActionResult> CreatePost([FromForm] CreatePostDto createPostDto)
        {
            var post = new Post();

            post.UserId = UserId;

            if (!String.IsNullOrEmpty(createPostDto.Text))
            {
                post.Text = createPostDto.Text;
            }

            if (createPostDto.Photo != null)
            {
                var uploadResultImage = new ImageUploadResult();

                using (var stream = createPostDto.Photo.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File = new FileDescription(createPostDto.Photo.Name, stream)
                    };
                    uploadResultImage = _cloudinary.Upload(uploadParams);
                }

                var photo = new Photo
                {
                    Id  = uploadResultImage.PublicId,
                    Url = uploadResultImage.Uri.ToString()
                };

                post.Photo = photo;
            }

            if (createPostDto.Video != null)
            {
                var uploadResultVideo = new VideoUploadResult();

                using (var stream = createPostDto.Video.OpenReadStream())
                {
                    var uploadParams = new VideoUploadParams()
                    {
                        File = new FileDescription(createPostDto.Video.Name, stream)
                    };
                    uploadResultVideo = _cloudinary.Upload(uploadParams);
                }

                var video = new Domain.Video
                {
                    Id  = uploadResultVideo.PublicId,
                    Url = uploadResultVideo.Uri.ToString()
                };

                post.Video = video;
            }

            _context.Posts.Add(post);
            await _context.SaveChangesAsync();

            var postDto = _mapper.Map <PostItemDto>(post);

            var userProfile = await _context.Profiles.Include(p => p.Avatar).FirstOrDefaultAsync(p => p.UserId == UserId);

            postDto.Avatar = userProfile.Avatar.Url;

            return(Ok(postDto));
        }
Ejemplo n.º 13
0
 public Task Commit()
 {
     return(_context.SaveChangesAsync());
 }