Beispiel #1
0
        public async Task <IActionResult> Create([Bind("FirstName,LastName,Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] AppUser appUser)
        {
            if (ModelState.IsValid)
            {
                _context.Add(appUser);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(appUser));
        }
        // Like/Dislike
        public async Task <IActionResult> Create(int PostId)
        {
            var UserId = _userManager.GetUserId(User);

            //Like likeExists = await _context.Likes.FindAsync(UserId, PostId);
            Like likeExists = _context.Likes.Where(l => l.UserId == UserId && l.PostId == PostId).FirstOrDefault();

            if (ModelState.IsValid)
            {
                if (likeExists == null)
                {
                    Like like = new Like {
                        UserId = UserId, PostId = PostId
                    };
                    _context.Add(like);
                }
                else
                {
                    _context.Remove(likeExists);
                }

                await _context.SaveChangesAsync();
            }

            return(RedirectToAction("Index", "Posts"));
        }
        public async Task <IActionResult> Create([Bind("PostId,Text,TypeId,ImageFile")] Post post)
        {
            if (ModelState.IsValid)
            {
                string wwwroot = _hostEnvironment.WebRootPath;

                AppUser AppUser = await _userManager.GetUserAsync(HttpContext.User);

                post.Owner       = AppUser;
                post.OwnerId     = AppUser.Id;
                post.DateCreated = DateTime.Now;

                if (post.ImageFile != null)
                {
                    string fileName  = Path.GetFileNameWithoutExtension(post.ImageFile.FileName);
                    string extension = Path.GetExtension(post.ImageFile.FileName);
                    post.Image = "postImage" + extension;
                }

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

                if (post.ImageFile != null)
                {
                    string path = Path.Combine(wwwroot, "userFiles", post.OwnerId, post.PostId + "", post.Image);
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                    using (var fileStream = new FileStream(path, FileMode.Create))
                    {
                        await post.ImageFile.CopyToAsync(fileStream);
                    }
                }

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["TypeId"] = new SelectList(_context.Interests, "InterestId", "Name", post.TypeId);

            return(View(post));
        }