delphimvcframework/samples/articles_crud_server/Controllers.Articles.pas

177 lines
4.3 KiB
ObjectPascal
Raw Normal View History

2014-06-27 15:30:39 +02:00
unit Controllers.Articles;
interface
uses
mvcframework,
mvcframework.Commons,
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
[MVCDoc('Resource that manages articles CRUD')]
2014-06-27 15:30:39 +02:00
[MVCPath('/articles')]
TArticlesController = class(TBaseController)
public
[MVCDoc('Returns the list of articles')]
2014-06-27 15:30:39 +02:00
[MVCPath]
[MVCHTTPMethod([httpGET])]
procedure GetArticles;
2017-02-06 12:02:02 +01: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);
[MVCDoc('Returns the article with the specified id')]
[MVCPath('/meta')]
[MVCHTTPMethod([httpGET])]
procedure GetArticleMeta;
[MVCDoc('Returns the article with the specified id')]
2014-06-27 15:30:39 +02:00
[MVCPath('/($id)')]
[MVCHTTPMethod([httpGET])]
procedure GetArticleByID(id: Integer);
[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])]
2023-09-01 12:49:10 +02:00
procedure UpdateArticleByID(const [MVCFromBody] Article: TArticle; const id: Integer);
[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 }
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;
procedure TArticlesController.DeleteArticleByID(id: Integer);
2014-06-27 15:30:39 +02:00
var
Article: TArticle;
begin
GetArticlesService.StartTransaction;
try
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;
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);
var
lDict: IMVCObjectDictionary;
begin
try
2023-09-01 12:49:10 +02:00
if Search = '' then
begin
lDict := ObjectDict().Add('data', GetArticlesService.GetAll);
end
else
begin
2023-09-01 12:49:10 +02:00
lDict := ObjectDict().Add('data', GetArticlesService.GetArticles(Search));
end;
Render(lDict);
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);
begin
2023-09-01 12:49:10 +02:00
Article.id := id;
GetArticlesService.Update(Article);
Render(200, 'Article Updated');
end;
procedure TArticlesController.GetArticleByID(id: Integer);
2014-06-27 15:30:39 +02:00
begin
try
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;
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.