@@ -16,7 +16,7 @@ public static void ConfigurePostEndpoints(this WebApplication app)
1616 var posts = app . MapGroup ( "posts" ) ;
1717 posts . MapPost ( "/" , CreatePost ) . WithSummary ( "Create post" ) ;
1818 posts . MapGet ( "/" , GetAllPosts ) . WithSummary ( "Get all posts" ) ;
19- posts . MapPatch ( "/{id}" , GetAllPosts ) . WithSummary ( "Update a certain post" ) ;
19+ posts . MapPatch ( "/{id}" , UpdatePost ) . WithSummary ( "Update a certain post" ) ;
2020 posts . MapDelete ( "/{id}" , GetAllPosts ) . WithSummary ( "Remove a certain post" ) ;
2121 }
2222 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
@@ -65,21 +65,28 @@ public static IResult GetAllPosts(IRepository<Post> service, IMapper mapper)
6565 }
6666
6767 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
68+ [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
6869 public static IResult UpdatePost ( IRepository < Post > service , IMapper mapper , int id , UpdatePostDTO request )
6970 {
70- if ( request . GetType ( ) . GetProperties ( ) . Length > 0 && request . GetType ( ) . GetProperties ( ) . All ( ( p ) => p . GetValue ( request ) == null ) ) return TypedResults . NoContent ( ) ;
71-
71+ if ( string . IsNullOrWhiteSpace ( request . Content ) ) return TypedResults . BadRequest ( new ResponseDTO < object > {
72+ Message = "Content cannot be empty"
73+ } ) ;
74+
75+ // TODO: Add new getbyid that uses includes
7276 var post = service . GetById ( id ) ;
7377
74- if ( post == null ) return TypedResults . NotFound ( new ResponseDTO < Object > { Message = "Missing post " } ) ;
78+ if ( post == null ) return TypedResults . NotFound ( new ResponseDTO < Object > { Message = "Post not found " } ) ;
7579
76- if ( string . IsNullOrEmpty ( request . Content ) ) post . Content = request . Content ;
80+ post . Content = request . Content ;
7781 post . UpdatedAt = DateTime . UtcNow ;
7882
7983 service . Update ( post ) ;
8084 service . Save ( ) ;
8185
82- return TypedResults . Ok ( request ) ;
86+ PostDTO postDTO = mapper . Map < PostDTO > ( post ) ;
87+
88+
89+ return TypedResults . Ok ( new ResponseDTO < PostDTO > { Message = "Success" , Data = postDTO } ) ;
8390 }
8491 }
8592}
0 commit comments