示例#1
0
        public async Task <IActionResult> Create(CreateUpdatePostViewModel model)
        {
            if (ModelState.IsValid)
            {
                AppUser CurrentUser = await this.UserManager.GetUserAsync(HttpContext.User);

                //Upload File
                String UploadPath = Path.Combine(HostingEnvironment.WebRootPath, "Uploads");
                if (!Directory.Exists(UploadPath))
                {
                    Directory.CreateDirectory(UploadPath);
                }
                String ThumbnailName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "-" + model.Thumbnail.FileName;
                String FullPath      = Path.Combine(UploadPath, ThumbnailName);
                using (FileStream stream = new FileStream(FullPath, FileMode.Create, FileAccess.Write))
                { model.Thumbnail.CopyTo(stream); }
                Post post = new Post()
                {
                    CategoryId    = model.CategoryId,
                    Status        = model.Status,
                    Content       = model.Content,
                    ThumbnailPath = ThumbnailName,
                    Title         = model.Title,
                    UserId        = CurrentUser.Id,
                };
                PostsRepository.Add(post);
                await PostsRepository.CommitAsync();

                return(RedirectToAction("Index"));
            }
            model.Categories = CategoriesRepository.GetAll();
            return(View(model));
        }
示例#2
0
        public bool Update(CreateUpdatePostViewModel model)
        {
            var post = model.AsPost();

            //_mapper.Map<Post>(model);
            //post.UserId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            return(_postRepository.Update(post));
        }
示例#3
0
        public long Create(CreateUpdatePostViewModel model)
        {
            var post = model.AsPost();

            post.Id             = 0;
            post.NotSoftDeleted = true;
            //_mapper.Map<Post>(model);
            post.CreationTime = DateTime.UtcNow;
            _postRepository.Add(post);
            return(post.Id);
        }
示例#4
0
 public async Task <IActionResult> Create([FromForm] CreateUpdatePostViewModel viewModel)
 {
     return(await Validation_EmailConfirmation_ModelState(User, ModelState, async (user) => {
         viewModel.UserId = user.Id;
         viewModel.Extension = Path.GetExtension(viewModel.File.FileName);
         var postId = postBehaviour.Create(viewModel);
         string extension = await photoManager.SavePostPhoto($"{postId}", viewModel.File);
         if (extension == null)
         {
             return Conflict("Could not save the file");
         }
         return StatusCode(201);
     }));
 }
示例#5
0
        public static CreateUpdatePostViewModel AsCreateUpdatePostViewModel(this Post post)
        {
            var viewModel = new CreateUpdatePostViewModel
            {
                Id       = post.Id,
                Title    = post.Title,
                BodyText = post.BodyText,
                SketchId = post.SketchId
            };

            //ViewModel.UserId
            //viewModel.File should be extracted in controller
            return(viewModel);
        }
示例#6
0
 public async Task <IActionResult> Update([FromForm] CreateUpdatePostViewModel viewModel)
 {
     return(await Validation_EmailConfirmation_ModelState(User, ModelState, async (user) => {
         if (postBehaviour.TheOwnerIs(viewModel.Id ?? -1, user.Id))
         {
             string extension = await photoManager.SavePostPhoto($"{viewModel.Id}", viewModel.File);
             if (extension == null)
             {
                 return Conflict("Could not save the file");
             }
             viewModel.UserId = user.Id;
             viewModel.Extension = extension;
             postBehaviour.Update(viewModel);
             return StatusCode(201);
         }
         return Unauthorized("Not owning this entity.");
     }));
 }
示例#7
0
        public static Post AsPost(this CreateUpdatePostViewModel viewModel)
        {
            var post = new Post
            {
                Id             = viewModel.Id ?? -1,
                Title          = viewModel.Title,
                UserId         = viewModel.UserId,
                Extension      = viewModel.Extension,
                BodyText       = viewModel.BodyText,
                NotSoftDeleted = true,
                SketchId       = viewModel.SketchId ?? 0
                                 //User = viewModel.User,
            };

            //post.CreationTime will be set in controller
            //post.File will be extracted in controller
            //Voters will eb set in controller
            return(post);
        }