Exemple #1
0
        public static Result <Post> Create(PostEditorDto editor)
        {
            Result result = Validate(editor);

            if (result.IsFailure)
            {
                return(Result.Fail <Post>(result.Error));
            }

            return(Result.Ok(new Post(editor)));
        }
Exemple #2
0
        public static Result Validate(PostEditorDto editor)
        {
            var autorValidationResult = ValidateAutor(editor.Autor);

            var titelValidationResult = ValidateTitle(editor.Title);

            var slugValidationResult = ValidateSlug(editor.Slug);

            // More validations if needed
            // ...

            return(Result.Combine(autorValidationResult, titelValidationResult, slugValidationResult));
        }
        public static PostEditorDto ToPostEditorDto(this PostEditorViewModel postEditorViewModel)
        {
            var postEditorDto = new PostEditorDto();

            postEditorDto.InjectFrom(postEditorViewModel);
            postEditorDto.HtmlContent = postEditorViewModel.ContentHtml;

            foreach (var tagName in postEditorViewModel.Tags.Split(PostEditorViewModel.TagSeparator).ToList())
            {
                postEditorDto.Tags.Add(tagName);
            }

            return(postEditorDto);
        }
Exemple #4
0
        public void MapEditorValues(PostEditorDto postEditor)
        {
            var validationResult = Validate(postEditor);

            if (validationResult.IsFailure)
            {
                throw new Exception(validationResult.Error);
            }

            Title       = postEditor.Title;
            Subtitle    = postEditor.Subtitle;
            Slug        = postEditor.Slug.Replace(" ", "-"); // Todo: Move to set propery? It would be compatible with EF?
            DatePost    = postEditor.DatePost;
            HtmlContent = postEditor.HtmlContent;
            Autor       = postEditor.Autor;
        }
Exemple #5
0
        public async Task <Result> UpdatePost(PostEditorDto editorPost)
        {
            var validationPostResult = Post.Validate(editorPost);

            if (validationPostResult.IsFailure)
            {
                return(validationPostResult);
            }

            Post post = await GetPostById(editorPost.Id);

            post.MapEditorValues(editorPost);

            _tagMapper.MapTagsToEntity(post, editorPost.Tags);

            await _db.SaveChangesAsync();

            return(Result.Ok());
        }
Exemple #6
0
        public async Task <Result> CreatePost(PostEditorDto editorPost)
        {
            var createPostResult = Post.Create(editorPost);

            if (createPostResult.IsFailure)
            {
                return(createPostResult);
            }

            Post post = createPostResult.Value;

            _tagMapper.MapTagsToEntity(post, editorPost.Tags);

            _db.Posts.Add(post);
            await _db.SaveChangesAsync();

            editorPost.Id = post.Id;

            return(Result.Ok());
        }
Exemple #7
0
 private Post(PostEditorDto postEditor) : this()
 {
     MapEditorValues(postEditor);
 }