Example #1
0
        public async Task SaveUserProfile_edit_user_success()
        {
            //arrange
            var userProfileDto = new UserProfileDto
            {
                PenName = "test1",
            };

            _httpContextAccessor.Setup(x => x.HttpContext.User.Identity.Name).Returns("*****@*****.**");

            var user = new UserProfile
            {
                Email   = "*****@*****.**",
                PenName = "test",
            };

            await _blogContext.AddAsync(user);

            await _blogContext.SaveChangesAsync();

            //act
            await _userProfileService.SaveUserProfile(userProfileDto);

            //assert
            var newUser = await _blogContext.UserProfiles.FirstOrDefaultAsync(u => u.Email == "*****@*****.**");

            Assert.Equal("test1", newUser.PenName);
        }
Example #2
0
        public async Task <IActionResult> AddCategory(Category category)
        {
            if (category.Id == 0)
            {
                await _context.AddAsync(category);
            }
            else
            {
                _context.Update(category);
            }
            await _context.SaveChangesAsync(); // Yaptığımız değişiklikleri veritabanına uygula.

            return(RedirectToAction(nameof(Category)));
        }
Example #3
0
        public async Task <IActionResult> AddCategory(Category category)
        {
            if (category.Id == 0)
            {
                await _context.AddAsync(category);
            }
            else
            {
                _context.Update(category);
            }
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Category)));
        }
Example #4
0
        public async Task <IActionResult> KategoriEkle(Kategori kategori)
        {
            if (kategori.Id == 0)
            {
                await _context.AddAsync(kategori);
            }
            else
            {
                _context.Update(kategori);
            }

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Kategori)));
        }
        public async Task AddAsync(T entity)
        {
            using var context = new BlogContext();
            await context.AddAsync(entity);

            await context.SaveChangesAsync();
        }
        public async Task Add(T item)
        {
            using BlogContext context = new BlogContext();
            await context.AddAsync(item);

            await context.SaveChangesAsync();
        }
Example #7
0
        public async Task <IActionResult> Save(Blog model)
        {
            if (model != null)
            {
                var file = Request.Form.Files.First();
                //C:\Users\aleyn\source\repos\MyBlog\wwwroot\img
                string savePath = Path.Combine("C:", "Users", "aleyn", "source", "repos", "MyBlog", "wwwroot", "img");
                var    fileName = $"{DateTime.Now:MMddHHmmss}.{file.FileName.Split(".").Last()}";
                var    fileUrl  = Path.Combine(savePath, fileName);

                using (var fileStream = new FileStream(fileUrl, FileMode.Create))
                {
                    await file.CopyToAsync(fileStream);
                }

                model.ImagePath = fileName;
                model.AuthorId  = (int)HttpContext.Session.GetInt32("id");

                await _context.AddAsync(model);

                await _context.SaveChangesAsync();

                return(Json(true));
            }
            return(Json(false));
        }
        public async Task <Guid?> SaveFile(IFormFile formFile)
        {
            if (formFile.Length <= 0)
            {
                return(null);
            }

            var newFileUpload = new FileUpload();

            await _blogContext.AddAsync(newFileUpload);

            await _blogContext.SaveChangesAsync();

            var fileExt  = Path.GetExtension(formFile.FileName);
            var fileName = $"{newFileUpload.Id.ToString()}{fileExt}";
            var filePath = Path.Combine(_config["StoredFilesPath"], fileName);

            Directory.CreateDirectory(_config["StoredFilesPath"]);
            using (var stream = File.Create(filePath))
            {
                await formFile.CopyToAsync(stream);
            }

            newFileUpload.FilePath  = filePath;
            newFileUpload.FileName  = fileName;
            newFileUpload.Extension = fileExt;

            await _blogContext.SaveChangesAsync();

            return(newFileUpload.Id);
        }
Example #9
0
        public async Task <int> InsertAsync(ImageStorage entity)
        {
            await _context.AddAsync(entity);

            await _context.SaveChangesAsync();

            return(entity.Id);
        }
            public async Task <Response> Handle(Request request, CancellationToken cancellationToken)
            {
                Blog post = Blog.CreateBlog(request.Title, request.Text);
                await _context.AddAsync(post);

                await _context.SaveChangesAsync();

                return(await Task.FromResult(new Response { Id = post.Id, Title = post.Title, Text = post.Text }));
            }
Example #11
0
        public async Task LikePost(int postId, string userId)
        {
            var like = new Like
            {
                PostId = postId,
                UserId = userId
            };

            await _context.AddAsync(like);

            await _context.SaveChangesAsync();
        }
        ///Yazar eklememizi sağlayan method.
        public async Task <IActionResult> AddAuthor(Author author)
        {
            if (author.Id == 0)
            {
                await _context.AddAsync(author);
            }
            else
            {
                _context.Update(author);
            }
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Author)));
        }
        public async Task <IActionResult> Save(Blog model)
        {
            if (model != null)
            {
                model.AuthorId = (int)HttpContext.Session.GetInt32("id");

                await _context.AddAsync(model);

                await _context.SaveChangesAsync();

                return(Json(true));
            }

            return(Json(false));
        }
Example #14
0
        public async Task <IActionResult> Seed(int number)
        {
            for (int i = 1; i < number; i++)
            {
                var post = new Post
                {
                    Title   = $"test {i}",
                    Content = "{\"blocks\":[{\"key\":\"9rsu9\",\"text\":\"test\",\"type\":\"unstyled\",\"depth\":0,\"inlineStyleRanges\":[],\"entityRanges\":[],\"data\":{}}],\"entityMap\":{}}",
                };
                await _blogContext.AddAsync(post);
            }
            await _blogContext.SaveChangesAsync();

            return(Ok());
        }
Example #15
0
        public async Task SaveUserProfile(UserProfileDto userProfileDto)
        {
            var authenticatedUserName = _httpContextAccessor.HttpContext.User.Identity.Name;

            var user = await _blogContext.UserProfiles.FirstOrDefaultAsync(u => u.Email == authenticatedUserName);

            if (user == null)
            {
                var newUser = new UserProfile
                {
                    Email   = authenticatedUserName,
                    PenName = userProfileDto.PenName,
                };
                await _blogContext.AddAsync(newUser);
            }
            else
            {
                user.Email   = authenticatedUserName;
                user.PenName = userProfileDto.PenName;
            }

            await _blogContext.SaveChangesAsync();
        }
Example #16
0
        public async Task <IActionResult> Kaydet(Blog model)
        {
            if (model != null)
            {
                var    file     = Request.Form.Files.First();
                string savePath = Path.Combine("C:", "Users", "Elif", "Desktop", "elifblog", "blog", "wwwroot", "img");
                var    fileName = $"{DateTime.Now:MMddHHmmss}.{file.FileName.Split(".").Last()}";
                var    fileUrl  = Path.Combine(savePath, fileName);
                using (var fileStream = new FileStream(fileUrl, FileMode.Create))
                {
                    await file.CopyToAsync(fileStream);
                }
                model.Resim   = fileName;
                model.YazarId = (int)HttpContext.Session.GetInt32("id");
                await _context.AddAsync(model);

                await _context.SaveChangesAsync();

                return(Json(true));
            }

            return(Json(false));
        }
Example #17
0
 public async Task AddTagAsync(TagDto tagDto)
 {
     await context.AddAsync(new TagTable { Name = tagDto.Name });
 }
Example #18
0
        public async Task SavePost(PostDto postDto)
        {
            var userName = _httpContextAccessor.HttpContext.User.Identity.Name;
            var author   = await _blogContext.UserProfiles.FirstOrDefaultAsync(u => u.Email == userName);

            var insertedTags = await _blogContext.Tags.Where(c => postDto.Tags.Contains(c.Name)).ToListAsync();

            var newTags = postDto.Tags.Except(insertedTags.Select(c => c.Name)).Select(x => new Tag {
                Name = x
            });

            if (!postDto.Id.HasValue)
            {
                var newPost = new Post
                {
                    Content          = postDto.Content,
                    Title            = postDto.Title,
                    ShortDescription = postDto.ShortDescription,
                    UrlSlug          = postDto.Title.GenerateSlug(),
                    CategoryId       = Guid.Parse(postDto.CategoryId),
                    CreatedDate      = DateTime.Now,
                    UpdatedDate      = DateTime.Now,
                    IsPublished      = postDto.IsPublished,
                    FileUploadId     = postDto.FileUploadId,
                    AuthorId         = author.Id
                };

                await _blogContext.AddAsync(newPost);

                await _blogContext.AddRangeAsync(newTags);

                await _blogContext.SaveChangesAsync();

                var postTags = new List <PostTag>();
                foreach (var newTag in newTags)
                {
                    var dbNewTag = await _blogContext.Tags.FirstOrDefaultAsync(t => t.Name == newTag.Name);

                    postTags.Add(new PostTag {
                        PostId = newPost.Id, TagId = dbNewTag.Id
                    });
                }

                foreach (var insertedTag in insertedTags)
                {
                    postTags.Add(new PostTag {
                        PostId = newPost.Id, TagId = insertedTag.Id
                    });
                }


                await _blogContext.AddRangeAsync(postTags);
            }
            else
            {
                var post = await _blogContext.Posts.Where(p => p.Id == postDto.Id.Value).FirstOrDefaultAsync();

                post.Content          = postDto.Content;
                post.Title            = postDto.Title;
                post.ShortDescription = postDto.ShortDescription;
                post.UrlSlug          = postDto.Title.GenerateSlug();
                post.UpdatedDate      = DateTime.Now;
                post.IsPublished      = postDto.IsPublished;
                if (postDto.FileUploadId.HasValue)
                {
                    post.FileUploadId = postDto.FileUploadId;
                }

                post.CategoryId = Guid.Parse(postDto.CategoryId);
                post.AuthorId   = author.Id;

                var oldPostTags = await _blogContext.PostTags.Where(p => p.PostId == post.Id).ToListAsync();

                _blogContext.PostTags.RemoveRange(oldPostTags);

                await _blogContext.AddRangeAsync(newTags);

                await _blogContext.SaveChangesAsync();

                var postTags = new List <PostTag>();
                foreach (var newTag in newTags)
                {
                    var dbNewTag = await _blogContext.Tags.FirstOrDefaultAsync(t => t.Name == newTag.Name);

                    postTags.Add(new PostTag {
                        PostId = post.Id, TagId = dbNewTag.Id
                    });
                }

                foreach (var insertedTag in insertedTags)
                {
                    postTags.Add(new PostTag {
                        PostId = post.Id, TagId = insertedTag.Id
                    });
                }

                await _blogContext.AddRangeAsync(postTags);
            }

            await _blogContext.SaveChangesAsync();
        }
        public async Task AddAsync(TEntity entity)
        {
            await _context.AddAsync(entity);

            await _context.SaveChangesAsync();
        }
Example #20
0
        public async Task <int> Add(TEntity model)
        {
            var res = await _mySqlContext.AddAsync <TEntity>(model);

            return(await _mySqlContext.SaveChangesAsync());
        }
        public async Task SavePost_add_new_post_success()
        {
            //arrange
            var fileUploadId = Guid.NewGuid();
            var user         = new UserProfile
            {
                Email   = "*****@*****.**",
                PenName = "test",
            };

            var category = new Category
            {
                Name = "category test",
            };

            await _blogContext.AddAsync(user);

            await _blogContext.AddAsync(category);

            await _blogContext.SaveChangesAsync();

            var categoryFromDb = await _blogContext.Categories.FirstOrDefaultAsync(c => c.Name == "category test");

            var postDto = new PostDto
            {
                Id               = null,
                Content          = "test",
                Title            = "test",
                ShortDescription = "test",
                Tags             = new List <string> {
                    "tag1"
                },
                CategoryId   = categoryFromDb.Id.ToString(),
                IsPublished  = true,
                FileUploadId = fileUploadId,
            };

            _httpContextAccessor.Setup(x => x.HttpContext.User.Identity.Name).Returns("*****@*****.**");

            //act
            await _postService.SavePost(postDto);

            //assert
            var newPost = await _blogContext.Posts.FirstOrDefaultAsync(u => u.Title == "test");

            var newTags = await _blogContext.Tags.ToListAsync();

            Assert.Equal("test", newPost.Title);
            Assert.Equal("test", newPost.Content);
            Assert.Equal("test", newPost.ShortDescription);
            Assert.Equal("tag1", newTags[0].Name);
            Assert.Equal(categoryFromDb.Id, newPost.CategoryId);
            Assert.True(newPost.IsPublished);
            Assert.Equal(fileUploadId, newPost.FileUploadId);
        }