2014-06-27 15:30:39 +02:00
|
|
|
unit Controllers.Articles;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
2019-06-24 20:59:33 +02:00
|
|
|
uses
|
|
|
|
mvcframework,
|
|
|
|
mvcframework.Commons,
|
2020-04-23 17:20:53 +02:00
|
|
|
mvcframework.Serializer.Commons,
|
2023-09-01 12:49:10 +02:00
|
|
|
System.Generics.Collections,
|
|
|
|
Controllers.Base, BusinessObjects;
|
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)
|
|
|
|
public
|
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])]
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure GetArticles;
|
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])]
|
2023-09-01 12:49:10 +02:00
|
|
|
procedure GetArticlesByDescription(const [MVCFromQueryString('q', '')] Search: String);
|
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])]
|
|
|
|
procedure GetArticleMeta;
|
|
|
|
|
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])]
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure GetArticleByID(id: Integer);
|
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])]
|
2023-09-01 12:49:10 +02:00
|
|
|
procedure UpdateArticleByID(const [MVCFromBody] Article: TArticle; const id: Integer);
|
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])]
|
2023-09-01 12:49:10 +02:00
|
|
|
procedure CreateArticle(const [MVCFromBody] Article: TArticle);
|
2018-12-12 11:00:41 +01:00
|
|
|
|
|
|
|
[MVCDoc('Creates new articles from a list and returns "201: Created"')]
|
|
|
|
[MVCPath('/bulk')]
|
|
|
|
[MVCHTTPMethod([httpPOST])]
|
2023-09-01 12:49:10 +02:00
|
|
|
procedure CreateArticles(const [MVCFromBody] ArticleList: TObjectList<TArticle>);
|
2014-06-27 15:30:39 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{ TArticlesController }
|
|
|
|
|
2019-06-24 20:59:33 +02:00
|
|
|
uses
|
|
|
|
Services,
|
|
|
|
Commons,
|
|
|
|
mvcframework.Serializer.Intf,
|
2023-09-01 12:49:10 +02:00
|
|
|
System.SysUtils;
|
2014-06-27 15:30:39 +02:00
|
|
|
|
2023-09-01 12:49:10 +02:00
|
|
|
procedure TArticlesController.CreateArticle(const Article: TArticle);
|
2014-06-27 15:30:39 +02:00
|
|
|
begin
|
2023-09-01 12:49:10 +02:00
|
|
|
GetArticlesService.Add(Article);
|
|
|
|
Render201Created('/articles/' + Article.id.ToString, 'Article Created');
|
2014-06-27 15:30:39 +02:00
|
|
|
end;
|
|
|
|
|
2023-09-01 12:49:10 +02:00
|
|
|
procedure TArticlesController.CreateArticles(const ArticleList: TObjectList<TArticle>);
|
2018-12-12 11:00:41 +01:00
|
|
|
var
|
|
|
|
lArticle: TArticle;
|
|
|
|
begin
|
2023-09-01 12:49:10 +02:00
|
|
|
GetArticlesService.StartTransaction;
|
2018-12-12 11:00:41 +01:00
|
|
|
try
|
2023-09-01 12:49:10 +02:00
|
|
|
for lArticle in ArticleList do
|
2018-12-12 11:00:41 +01:00
|
|
|
begin
|
|
|
|
GetArticlesService.Add(lArticle);
|
|
|
|
end;
|
2023-09-01 12:49:10 +02:00
|
|
|
GetArticlesService.Commit;
|
|
|
|
except
|
|
|
|
GetArticlesService.Rollback;
|
|
|
|
raise;
|
2018-12-12 11:00:41 +01:00
|
|
|
end;
|
2023-09-01 12:49:10 +02:00
|
|
|
Render(201, 'Articles Created');
|
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
|
|
|
var
|
|
|
|
Article: TArticle;
|
|
|
|
begin
|
|
|
|
GetArticlesService.StartTransaction;
|
|
|
|
try
|
2017-05-12 00:25:46 +02:00
|
|
|
Article := GetArticlesService.GetByID(id);
|
2014-06-27 15:30:39 +02:00
|
|
|
try
|
|
|
|
GetArticlesService.Delete(Article);
|
|
|
|
finally
|
|
|
|
Article.Free;
|
|
|
|
end;
|
|
|
|
GetArticlesService.Commit;
|
|
|
|
except
|
|
|
|
GetArticlesService.Rollback;
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure TArticlesController.GetArticles;
|
2014-06-27 15:30:39 +02:00
|
|
|
begin
|
2023-09-01 12:49:10 +02:00
|
|
|
Render(ObjectDict().Add('data', GetArticlesService.GetAll));
|
2014-06-27 15:30:39 +02:00
|
|
|
end;
|
|
|
|
|
2023-09-01 12:49:10 +02:00
|
|
|
procedure TArticlesController.GetArticlesByDescription(const Search: String);
|
2019-06-24 20:59:33 +02:00
|
|
|
var
|
2020-04-23 17:20:53 +02:00
|
|
|
lDict: IMVCObjectDictionary;
|
2019-06-24 20:59:33 +02:00
|
|
|
begin
|
|
|
|
try
|
2023-09-01 12:49:10 +02:00
|
|
|
if Search = '' then
|
2020-04-23 17:20:53 +02:00
|
|
|
begin
|
|
|
|
lDict := ObjectDict().Add('data', GetArticlesService.GetAll);
|
|
|
|
end
|
2019-06-24 20:59:33 +02:00
|
|
|
else
|
2020-04-23 17:20:53 +02:00
|
|
|
begin
|
2023-09-01 12:49:10 +02:00
|
|
|
lDict := ObjectDict().Add('data', GetArticlesService.GetArticles(Search));
|
2020-04-23 17:20:53 +02:00
|
|
|
end;
|
|
|
|
Render(lDict);
|
2019-06-24 20:59:33 +02:00
|
|
|
except
|
|
|
|
on E: EServiceException do
|
|
|
|
begin
|
|
|
|
raise EMVCException.Create(E.Message, '', 0, 404);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2023-09-01 12:49:10 +02:00
|
|
|
procedure TArticlesController.UpdateArticleByID(const Article: TArticle; const id: Integer);
|
2014-06-27 16:38:49 +02:00
|
|
|
begin
|
2023-09-01 12:49:10 +02:00
|
|
|
Article.id := id;
|
|
|
|
GetArticlesService.Update(Article);
|
|
|
|
Render(200, 'Article Updated');
|
2014-06-27 16:38:49 +02:00
|
|
|
end;
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure TArticlesController.GetArticleByID(id: Integer);
|
2014-06-27 15:30:39 +02:00
|
|
|
begin
|
|
|
|
try
|
2020-04-23 17:20:53 +02:00
|
|
|
Render(ObjectDict().Add('data', GetArticlesService.GetByID(id)));
|
2014-06-27 15:30:39 +02:00
|
|
|
except
|
|
|
|
on E: EServiceException do
|
|
|
|
begin
|
|
|
|
raise EMVCException.Create(E.Message, '', 0, 404);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-09-23 23:33:30 +02:00
|
|
|
procedure TArticlesController.GetArticleMeta;
|
|
|
|
begin
|
|
|
|
try
|
|
|
|
Render(ObjectDict().Add('data', GetArticlesService.GetMeta));
|
|
|
|
except
|
|
|
|
on E: EServiceException do
|
|
|
|
begin
|
|
|
|
raise EMVCException.Create(E.Message, '', 0, 404);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-06-27 15:30:39 +02:00
|
|
|
end.
|