示例#1
0
        public IActionResult Create()
        {
            PublicationCreateViewModel model = new PublicationCreateViewModel
            {
                User = _db.Users.FirstOrDefault(p => p.UserName == User.Identity.Name)
            };

            return(View(model));
        }
示例#2
0
        public string Load(PublicationCreateViewModel model, User user)
        {
            string userFile      = $"{user.UserName}_{user.Email}";
            string imageFileName = Guid.NewGuid().ToString();
            string path          = Path.Combine(_environment.ContentRootPath + $"\\wwwroot\\UserFiles\\{userFile}\\{imageFileName}");
            string photoPath     = $"UserFiles/{userFile}/{imageFileName}/{model.Image.FileName}";

            Directory.CreateDirectory($"wwwroot/UserFiles/{userFile}/{imageFileName}");
            _uploadService.Upload(path, model.Image.FileName, model.Image);
            return(photoPath);
        }
示例#3
0
        public async Task <IActionResult> Create(PublicationCreateViewModel model)
        {
            if (model.Image is null)
            {
                ModelState.AddModelError("", "В публикации должно быть фото");
            }
            else if (model.Image.ContentType != "image/jpg" && model.Image.ContentType != "image/png" && model.Image.ContentType != "image/jpeg")
            {
                ModelState.AddModelError("", "В публикации должно быть фото");
            }
            else if (ModelState.IsValid)
            {
                Publication publication = new Publication();
                User        user        = await _userService.GetUserByUserName(User.Identity.Name);

                publication.AuthorId     = user.Id;
                publication.Inscription  = model.Inscription;
                publication.ImagePath    = Load(model, user);
                publication.CreationDate = DateTime.Now;
                publication.Id           = Guid.NewGuid().ToString();
                publication.LikesIds     = new List <string>();
                publication.CommentIds   = new List <string>();
                await _db.Publications.AddAsync(publication);

                await _db.SaveChangesAsync();

                Publication pub = _db.Publications.FirstOrDefault(p => p.ImagePath == publication.ImagePath);
                user.PublicationIds.Add(pub.Id);
                _db.Entry(user).State = EntityState.Detached;
                _db.Entry(user).State = EntityState.Modified;
                await _db.SaveChangesAsync();

                _userService.AddUserToCache(user);
                return(RedirectToAction("Details", "Account"));
            }
            model.User = _db.Users.FirstOrDefault(p => p.UserName == User.Identity.Name);
            return(View(model));
        }
示例#4
0
        public async Task <IActionResult> AddPost([FromRoute] int blogId, [FromBody] PublicationCreateViewModel model)
        {
            var blog = await CheckBlog(blogId);

            if (blog == null)
            {
                return(Forbid());
            }

            var sub    = User.FindFirst("sub").Value;
            var entity = await _context.Publications.AddAsync(new Post
            {
                AuthorSub = sub,
                BlogId    = blogId,
                Body      = model.Body,
                Title     = model.Title
            });

            await _context.SaveChangesAsync();

            await entity.Reference(x => x.AuthorProfile).LoadAsync();

            return(Ok(entity.Entity.ToPublicationViewModel()));
        }