public ActionResult Create([Bind(Include = "ID,Email,Password")] User user)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(user));
        }
Exemple #2
0
        public async Task AddFriendAsync(int userId, int friendId)
        {
            var friend = await GetById(friendId);

            var currentUser = await GetById(userId);

            var friendshipsCount = db.Friendships.Where(x =>
                                                        x.FriendId == friendId && x.UserId == currentUser.Id ||
                                                        (x.FriendId == currentUser.Id && x.UserId == friendId)).Count();

            if (friendshipsCount == 0)
            {
                if (friend != null && currentUser != null)
                {
                    friend.UserFriends.Add(new Friends {
                        UserId = friendId, FriendId = userId
                    });
                    currentUser.UserFriends.Add(new Friends {
                        UserId = userId, FriendId = friendId
                    });
                    db.SaveChanges();
                }
                else
                {
                    //обработать ошибку отсутствующего юзера
                }
            }
            else
            {
                //обработать ошибку если юзер уже добавлен
            }
        }
Exemple #3
0
 public UserController(DBUserContext context, IUserService userService, IHttpContextAccessor httpContextAccessor)
 {
     _db          = context;
     _userService = userService;
     if (!_db.Users.Any())
     {
         _db.Users.Add(new User {
             UserName = "******", Password = "******", AvatarImgPath = @"Resources\Images\AnonimPage.jpg"
         });
         _db.Users.Add(new User {
             UserName = "******", Password = "******", AvatarImgPath = @"Resources\Images\AnonimPage.jpg"
         });
         _db.Users.Add(new User {
             UserName = "******", Password = "******", AvatarImgPath = @"Resources\Images\AnonimPage.jpg"
         });
         _db.SaveChanges();
     }
 }