delphimvcframework/samples/articles_crud_server/Controllers.Articles.pas

136 lines
3.8 KiB
ObjectPascal
Raw Normal View History

2014-06-27 15:30:39 +02:00
unit Controllers.Articles;
interface
uses
MVCFramework,
MVCFramework.Commons,
2023-09-01 12:49:10 +02:00
System.Generics.Collections,
Controllers.Base,
BusinessObjects,
Services;
2014-06-27 15:30:39 +02:00
type
[MVCDoc('Resource that manages articles CRUD')]
2014-06-27 15:30:39 +02:00
[MVCPath('/articles')]
TArticlesController = class(TBaseController)
private
fArticlesService: IArticlesService;
2014-06-27 15:30:39 +02:00
public
[MVCInject]
constructor Create(ArticlesService: IArticlesService); reintroduce;
[MVCDoc('Returns the list of articles')]
2014-06-27 15:30:39 +02:00
[MVCPath]
[MVCHTTPMethod([httpGET])]
2024-03-31 18:37:09 +02:00
function GetArticles: IMVCResponse;
2017-02-06 12:02:02 +01:00
[MVCDoc('Returns the list of articles')]
[MVCPath('/searches')]
[MVCHTTPMethod([httpGET])]
2024-03-31 18:37:09 +02:00
function GetArticlesByDescription(const [MVCFromQueryString('q', '')] Search: String): IMVCResponse;
[MVCDoc('Returns the article with the specified id')]
[MVCPath('/meta')]
[MVCHTTPMethod([httpGET])]
2024-03-31 18:37:09 +02:00
function GetArticleMeta: IMVCResponse;
[MVCDoc('Returns the article with the specified id')]
2014-06-27 15:30:39 +02:00
[MVCPath('/($id)')]
[MVCHTTPMethod([httpGET])]
2024-03-31 18:37:09 +02:00
function GetArticleByID(id: Integer): IMVCResponse;
[MVCDoc('Deletes the article with the specified id')]
2014-06-27 15:30:39 +02:00
[MVCPath('/($id)')]
[MVCHTTPMethod([httpDelete])]
procedure DeleteArticleByID(id: Integer);
[MVCDoc('Updates the article with the specified id and return "200: OK"')]
[MVCPath('/($id)')]
[MVCHTTPMethod([httpPUT])]
2024-03-31 18:37:09 +02:00
function UpdateArticleByID(const [MVCFromBody] Article: TArticle; const id: Integer): IMVCResponse;
[MVCDoc('Creates a new article and returns "201: Created"')]
2014-06-27 15:30:39 +02:00
[MVCPath]
[MVCHTTPMethod([httpPOST])]
2024-03-31 18:37:09 +02:00
function CreateArticle(const [MVCFromBody] Article: TArticle): IMVCResponse;
2018-12-12 11:00:41 +01:00
[MVCDoc('Creates new articles from a list and returns "201: Created"')]
[MVCPath('/bulk')]
[MVCHTTPMethod([httpPOST])]
2024-03-31 18:37:09 +02:00
function CreateArticles(const [MVCFromBody] ArticleList: TObjectList<TArticle>): IMVCResponse;
2014-06-27 15:30:39 +02:00
end;
implementation
{ TArticlesController }
uses
2023-09-01 12:49:10 +02:00
System.SysUtils;
2014-06-27 15:30:39 +02:00
constructor TArticlesController.Create(ArticlesService: IArticlesService);
begin
inherited Create;
fArticlesService := ArticlesService;
end;
2024-03-31 18:37:09 +02:00
function TArticlesController.CreateArticle(const Article: TArticle): IMVCResponse;
2014-06-27 15:30:39 +02:00
begin
fArticlesService.Add(Article);
2023-09-01 12:49:10 +02:00
Render201Created('/articles/' + Article.id.ToString, 'Article Created');
2014-06-27 15:30:39 +02:00
end;
2024-03-31 18:37:09 +02:00
function TArticlesController.CreateArticles(const ArticleList: TObjectList<TArticle>): IMVCResponse;
2018-12-12 11:00:41 +01:00
begin
2024-03-31 18:37:09 +02:00
fArticlesService.CreateArticles(ArticleList);
Result := MVCResponseBuilder.StatusCode(HTTP_STATUS.Created).Build;
2018-12-12 11:00:41 +01:00
end;
procedure TArticlesController.DeleteArticleByID(id: Integer);
2014-06-27 15:30:39 +02:00
begin
fArticlesService.Delete(fArticlesService.GetByID(id));
2014-06-27 15:30:39 +02:00
end;
2024-03-31 18:37:09 +02:00
function TArticlesController.GetArticles: IMVCResponse;
2014-06-27 15:30:39 +02:00
begin
2024-03-31 18:37:09 +02:00
Result := MVCResponseBuilder
.StatusCode(HTTP_STATUS.OK)
.Body(fArticlesService.GetAll)
.Build;
2014-06-27 15:30:39 +02:00
end;
2024-03-31 18:37:09 +02:00
function TArticlesController.GetArticlesByDescription(const Search: String): IMVCResponse;
begin
2024-03-31 18:37:09 +02:00
Result := MVCResponseBuilder
.StatusCode(HTTP_STATUS.OK)
.Body(fArticlesService.GetArticles(Search))
.Build;
end;
2024-03-31 18:37:09 +02:00
function TArticlesController.UpdateArticleByID(const Article: TArticle; const id: Integer): IMVCResponse;
begin
2023-09-01 12:49:10 +02:00
Article.id := id;
fArticlesService.Update(Article);
Result := MVCResponseBuilder
.StatusCode(HTTP_STATUS.OK)
.Build;
end;
2024-03-31 18:37:09 +02:00
function TArticlesController.GetArticleByID(id: Integer): IMVCResponse;
2014-06-27 15:30:39 +02:00
begin
2024-03-31 18:37:09 +02:00
Result := MVCResponseBuilder
.StatusCode(HTTP_STATUS.OK)
.Body(fArticlesService.GetByID(id))
.Build;
2014-06-27 15:30:39 +02:00
end;
2024-03-31 18:37:09 +02:00
function TArticlesController.GetArticleMeta: IMVCResponse;
begin
2024-03-31 18:37:09 +02:00
Result := MVCResponseBuilder
.StatusCode(HTTP_STATUS.OK)
.Body(fArticlesService.GetMeta)
.Build;
end;
2014-06-27 15:30:39 +02:00
end.