2014-06-27 15:30:39 +02:00
|
|
|
unit Controllers.Articles;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
2019-06-24 20:59:33 +02:00
|
|
|
uses
|
2024-04-03 14:41:29 +02:00
|
|
|
MVCFramework,
|
|
|
|
MVCFramework.Commons,
|
2023-09-01 12:49:10 +02:00
|
|
|
System.Generics.Collections,
|
2024-04-03 14:41:29 +02:00
|
|
|
Controllers.Base,
|
|
|
|
BusinessObjects,
|
|
|
|
Services;
|
2014-06-27 15:30:39 +02:00
|
|
|
|
|
|
|
type
|
2016-11-24 20:07:45 +01:00
|
|
|
[MVCDoc('Resource that manages articles CRUD')]
|
2014-06-27 15:30:39 +02:00
|
|
|
[MVCPath('/articles')]
|
|
|
|
TArticlesController = class(TBaseController)
|
2024-03-30 00:30:14 +01:00
|
|
|
private
|
|
|
|
fArticlesService: IArticlesService;
|
2014-06-27 15:30:39 +02:00
|
|
|
public
|
2024-03-30 00:30:14 +01:00
|
|
|
[MVCInject]
|
|
|
|
constructor Create(ArticlesService: IArticlesService); reintroduce;
|
|
|
|
|
2016-11-24 20:07:45 +01:00
|
|
|
[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
|
|
|
|
2019-06-24 20:59:33 +02: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;
|
2019-06-24 20:59:33 +02:00
|
|
|
|
2020-09-23 23:33:30 +02:00
|
|
|
[MVCDoc('Returns the article with the specified id')]
|
|
|
|
[MVCPath('/meta')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2024-03-31 18:37:09 +02:00
|
|
|
function GetArticleMeta: IMVCResponse;
|
2020-09-23 23:33:30 +02:00
|
|
|
|
2016-11-24 20:07:45 +01:00
|
|
|
[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;
|
2017-05-17 22:32:45 +02:00
|
|
|
|
2016-11-24 20:07:45 +01:00
|
|
|
[MVCDoc('Deletes the article with the specified id')]
|
2014-06-27 15:30:39 +02:00
|
|
|
[MVCPath('/($id)')]
|
|
|
|
[MVCHTTPMethod([httpDelete])]
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure DeleteArticleByID(id: Integer);
|
2016-11-24 20:07:45 +01:00
|
|
|
|
|
|
|
[MVCDoc('Updates the article with the specified id and return "200: OK"')]
|
2014-06-27 16:38:49 +02:00
|
|
|
[MVCPath('/($id)')]
|
|
|
|
[MVCHTTPMethod([httpPUT])]
|
2024-03-31 18:37:09 +02:00
|
|
|
function UpdateArticleByID(const [MVCFromBody] Article: TArticle; const id: Integer): IMVCResponse;
|
2016-11-24 20:07:45 +01:00
|
|
|
|
|
|
|
[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 }
|
|
|
|
|
2019-06-24 20:59:33 +02:00
|
|
|
uses
|
2023-09-01 12:49:10 +02:00
|
|
|
System.SysUtils;
|
2014-06-27 15:30:39 +02:00
|
|
|
|
2024-03-30 00:30:14 +01: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
|
2024-03-30 00:30:14 +01:00
|
|
|
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;
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure TArticlesController.DeleteArticleByID(id: Integer);
|
2014-06-27 15:30:39 +02:00
|
|
|
begin
|
2024-04-03 14:41:29 +02:00
|
|
|
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;
|
2019-06-24 20:59:33 +02:00
|
|
|
begin
|
2024-03-31 18:37:09 +02:00
|
|
|
Result := MVCResponseBuilder
|
|
|
|
.StatusCode(HTTP_STATUS.OK)
|
|
|
|
.Body(fArticlesService.GetArticles(Search))
|
|
|
|
.Build;
|
2019-06-24 20:59:33 +02:00
|
|
|
end;
|
|
|
|
|
2024-03-31 18:37:09 +02:00
|
|
|
function TArticlesController.UpdateArticleByID(const Article: TArticle; const id: Integer): IMVCResponse;
|
2014-06-27 16:38:49 +02:00
|
|
|
begin
|
2023-09-01 12:49:10 +02:00
|
|
|
Article.id := id;
|
2024-03-30 00:30:14 +01:00
|
|
|
fArticlesService.Update(Article);
|
2024-04-03 14:41:29 +02:00
|
|
|
Result := MVCResponseBuilder
|
|
|
|
.StatusCode(HTTP_STATUS.OK)
|
|
|
|
.Build;
|
2014-06-27 16:38:49 +02:00
|
|
|
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;
|
2020-09-23 23:33:30 +02:00
|
|
|
begin
|
2024-03-31 18:37:09 +02:00
|
|
|
Result := MVCResponseBuilder
|
|
|
|
.StatusCode(HTTP_STATUS.OK)
|
|
|
|
.Body(fArticlesService.GetMeta)
|
|
|
|
.Build;
|
2020-09-23 23:33:30 +02:00
|
|
|
end;
|
|
|
|
|
2014-06-27 15:30:39 +02:00
|
|
|
end.
|