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,
|
2019-06-24 20:59:33 +02:00
|
|
|
Controllers.Base;
|
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])]
|
|
|
|
procedure GetArticlesByDescription;
|
|
|
|
|
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])]
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure UpdateArticleByID(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])]
|
2020-01-24 10:09:14 +01:00
|
|
|
procedure CreateArticle;
|
2018-12-12 11:00:41 +01:00
|
|
|
|
|
|
|
[MVCDoc('Creates new articles from a list and returns "201: Created"')]
|
|
|
|
[MVCPath('/bulk')]
|
|
|
|
[MVCHTTPMethod([httpPOST])]
|
2020-01-24 10:09:14 +01:00
|
|
|
procedure CreateArticles;
|
2014-06-27 15:30:39 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{ TArticlesController }
|
|
|
|
|
2019-06-24 20:59:33 +02:00
|
|
|
uses
|
|
|
|
Services,
|
|
|
|
BusinessObjects,
|
|
|
|
Commons,
|
|
|
|
mvcframework.Serializer.Intf,
|
2020-01-24 10:09:14 +01:00
|
|
|
System.Generics.Collections, System.SysUtils;
|
2014-06-27 15:30:39 +02:00
|
|
|
|
2020-01-24 10:09:14 +01:00
|
|
|
procedure TArticlesController.CreateArticle;
|
2014-06-27 15:30:39 +02:00
|
|
|
var
|
|
|
|
Article: TArticle;
|
|
|
|
begin
|
2014-06-27 16:38:49 +02:00
|
|
|
Article := Context.Request.BodyAs<TArticle>;
|
2014-06-27 15:30:39 +02:00
|
|
|
try
|
|
|
|
GetArticlesService.Add(Article);
|
2020-04-23 17:20:53 +02:00
|
|
|
Render201Created('/articles/' + Article.id.ToString, 'Article Created');
|
2014-06-27 15:30:39 +02:00
|
|
|
finally
|
|
|
|
Article.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-01-24 10:09:14 +01:00
|
|
|
procedure TArticlesController.CreateArticles;
|
2018-12-12 11:00:41 +01:00
|
|
|
var
|
|
|
|
lArticles: TObjectList<TArticle>;
|
|
|
|
lArticle: TArticle;
|
|
|
|
begin
|
|
|
|
lArticles := Context.Request.BodyAsListOf<TArticle>;
|
|
|
|
try
|
|
|
|
for lArticle in lArticles do
|
|
|
|
begin
|
|
|
|
GetArticlesService.Add(lArticle);
|
|
|
|
end;
|
|
|
|
Render(201, 'Articles Created');
|
|
|
|
finally
|
|
|
|
lArticles.Free;
|
|
|
|
end;
|
|
|
|
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
|
2020-04-23 17:20:53 +02:00
|
|
|
Render(
|
|
|
|
ObjectDict().Add('data', GetArticlesService.GetAll)
|
|
|
|
);
|
2014-06-27 15:30:39 +02:00
|
|
|
end;
|
|
|
|
|
2019-06-24 20:59:33 +02:00
|
|
|
procedure TArticlesController.GetArticlesByDescription;
|
|
|
|
var
|
2020-04-23 17:20:53 +02:00
|
|
|
lSearch: string;
|
|
|
|
lDict: IMVCObjectDictionary;
|
2019-06-24 20:59:33 +02:00
|
|
|
begin
|
|
|
|
try
|
|
|
|
lSearch := Context.Request.Params['q'];
|
|
|
|
if lSearch = '' 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
|
|
|
|
lDict := ObjectDict().Add('data', GetArticlesService.GetArticles(lSearch));
|
|
|
|
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;
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure TArticlesController.UpdateArticleByID(id: Integer);
|
2014-06-27 16:38:49 +02:00
|
|
|
var
|
|
|
|
Article: TArticle;
|
|
|
|
begin
|
|
|
|
Article := Context.Request.BodyAs<TArticle>;
|
|
|
|
try
|
2017-05-12 00:25:46 +02:00
|
|
|
Article.id := id;
|
2014-06-27 16:38:49 +02:00
|
|
|
GetArticlesService.Update(Article);
|
2016-11-24 20:07:45 +01:00
|
|
|
Render(200, 'Article Updated');
|
2014-06-27 16:38:49 +02:00
|
|
|
finally
|
|
|
|
Article.Free;
|
|
|
|
end;
|
|
|
|
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.
|