예제 #1
0
        public void CreatePost(PostRequestDto postRequestDto)
        {
            var post = new Post
            {
                Title      = postRequestDto.Title,
                Content    = postRequestDto.Content,
                Likes      = postRequestDto.Likes,
                Dislikes   = postRequestDto.Dislikes,
                CategoryId = 5
            };

            _context.Posts.Add(post);
            _context.SaveChanges();

            var postId = post.Id;

            var imageNameWithoutExtension = Path.GetFileNameWithoutExtension(postRequestDto.ImageFile.FileName);
            var imageExtension            = Path.GetExtension(postRequestDto.ImageFile.FileName);
            var imageName = imageNameWithoutExtension + imageExtension;

            var pathToImage = "~/UploadedImages/" + imageName;

            var imagePath = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedImages/"), imageName);

            postRequestDto.ImageFile.SaveAs(imagePath);

            var image = new Image
            {
                Name      = imageName,
                Url       = pathToImage,
                Extension = imageExtension,
                postId    = postId
            };

            _context.Images.Add(image);
            _context.SaveChanges();
        }
예제 #2
0
 public void SaveChanges()
 {
     _context.SaveChanges();
 }