delphimvcframework/samples/articles_crud_server/Controllers.Articles.pas

194 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,
Controllers.Base;
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])]
procedure GetArticlesByDescription;
[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])]
procedure UpdateArticleByID(id: Integer);
[MVCDoc('Creates a new article and returns "201: Created"')]
2014-06-27 15:30:39 +02:00
[MVCPath]
[MVCHTTPMethod([httpPOST])]
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])]
procedure CreateArticles;
2014-06-27 15:30:39 +02:00
end;
implementation
{ TArticlesController }
uses
Services,
BusinessObjects,
Commons,
mvcframework.Serializer.Intf,
System.Generics.Collections, System.SysUtils;
2014-06-27 15:30:39 +02:00
procedure TArticlesController.CreateArticle;
2014-06-27 15:30:39 +02:00
var
Article: TArticle;
begin
Article := Context.Request.BodyAs<TArticle>;
2014-06-27 15:30:39 +02:00
try
GetArticlesService.Add(Article);
Render201Created('/articles/' + Article.id.ToString, 'Article Created');
2014-06-27 15:30:39 +02:00
finally
Article.Free;
end;
end;
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;
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
Render(
ObjectDict().Add('data', GetArticlesService.GetAll)
);
2014-06-27 15:30:39 +02:00
end;
procedure TArticlesController.GetArticlesByDescription;
var
lSearch: string;
lDict: IMVCObjectDictionary;
begin
try
lSearch := Context.Request.Params['q'];
if lSearch = '' then
begin
lDict := ObjectDict().Add('data', GetArticlesService.GetAll);
end
else
begin
lDict := ObjectDict().Add('data', GetArticlesService.GetArticles(lSearch));
end;
Render(lDict);
except
on E: EServiceException do
begin
raise EMVCException.Create(E.Message, '', 0, 404);
end
else
raise;
end;
end;
procedure TArticlesController.UpdateArticleByID(id: Integer);
var
Article: TArticle;
begin
Article := Context.Request.BodyAs<TArticle>;
try
Article.id := id;
GetArticlesService.Update(Article);
Render(200, 'Article Updated');
finally
Article.Free;
end;
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.