예제 #1
0
        public async Task <IActionResult> CreatePost(PostCreateDTO postCreateDTO)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.FindByNameAsync(User.Identity.Name);

                var categories = await _categoryService.GetAllAsync();

                var languages = await _languageService.GetAllAsync();

                var category = categories.First();
                var language = languages.First();

                var post = _mapper.Map <Post>(postCreateDTO);

                post.UserId     = user.Id;
                post.Created    = DateTime.Now;
                post.CreatedBy  = user.UserName;
                post.PostStatus = PostStatus.Posted;
                post.CategoryId = category.Id;
                post.LanguageId = language.Id;

                var newPost = await _postService.CreateAsync(post);

                if (postCreateDTO.Files != null)
                {
                    List <Picture> pictures = AddFiles(postCreateDTO.Files, newPost.Id, user.UserName);

                    foreach (var picture in pictures)
                    {
                        await _pictureService.CreateAsync(picture);
                    }
                }

                var usersForNotification = await _userService.GetUsersForNitificationAsync();

                if (usersForNotification != null)
                {
                    var usersVoewDTO = _mapper.Map <List <UserViewDto> >(usersForNotification);
                    var postViewDTO  = _mapper.Map <PostViewDTO>(post);
                    var userViewDTO  = _mapper.Map <UserViewDto>(user);

                    postViewDTO.UserViewDto = userViewDTO;

                    var viewPostLink = Url.Action(
                        "PostDetails",
                        "Post",
                        new { id = postViewDTO.Id },
                        protocol: HttpContext.Request.Scheme);

                    var stringForm = await _razorViewToStringRenderer.RenderToStringAsync("Post/PostForEmailNotification", postViewDTO);

                    await _automaticEmailNotificationService.SentAutomaticNotificationAsync(EmailNotificationSettings.subject,
                                                                                            EmailNotificationSettings.CteateMessage(postViewDTO, stringForm, viewPostLink), usersVoewDTO);
                }

                return(RedirectToAction("Index", "Home"));
            }
            return(View(postCreateDTO));
        }