Exemple #1
0
        public async Task <Post> CreatePost(CreatePostDto input)
        {
            var com = await _communityRepository.GetAll().FirstOrDefaultAsync(x => x.Slug == input.CommunitySlug);

            var noHtml  = Slug.HtmlToPlainText(input.Content);
            var slug    = Slug.FriendlyUrlTitle(noHtml);
            var hasSlug = await _postRepository.GetAll()
                          .FirstOrDefaultAsync(x => x.IsDeleted == false && x.Slug == slug);

            if (hasSlug != null)
            {
                var newContent = input.Content + "" + RandomString.GenerateString(3);
                slug = Slug.FriendlyUrlTitle(newContent);
            }
            var post = new Post
            {
                Content     = input.Content,
                CommunityId = com.Id,
                UserId      = input.UserId,
                ContentType = input.ContentType,
                Slug        = slug
            };

            var user = await _userRepository.GetByIdAsync(input.UserId);

            if (input.ContentFile != null)
            {
                var path = await _blobService.InsertFile(input.ContentFile);

                post.MediaContentPath = path;
            }

            if (input.ContentType == ContentType.Link || input.ContentType == ContentType.YoutubeLink)
            {
                post.LinkUrl = input.LinkUrl;
            }
            await _postRepository.AddAsync(post);

            var mods = await _communityUserRepository.GetAll().Include(x => x.User).Where(x =>
                                                                                          !x.IsDeleted && x.IsAdmin && x.CommunityId == com.Id && x.UserId != input.UserId)
                       .ToListAsync();

            var msg     = user.Username + " moderatörü olduğun" + " " + com.Name + " " + "topluluğunda paylaşım yaptı." + " " + "https://saalla.com/t/" + com.Slug;
            var subject = "Bir yeni paylaşım";

            foreach (var mod in mods)
            {
                await _emailSender.SendEmail(mod.User.EmailAddress, subject, msg);
            }

            return(post);
        }