delphimvcframework/samples/articles_crud_server/Controllers.Articles.pas

172 lines
3.8 KiB
ObjectPascal
Raw Normal View History

2014-06-27 15:30:39 +02:00
unit Controllers.Articles;
interface
uses
mvcframework,
mvcframework.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')]
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(Context: TWebContext);
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(Context: TWebContext);
2014-06-27 15:30:39 +02:00
end;
implementation
{ TArticlesController }
uses
Services,
BusinessObjects,
Commons,
mvcframework.Serializer.Intf,
2018-12-12 11:00:41 +01:00
System.Generics.Collections;
2014-06-27 15:30:39 +02:00
procedure TArticlesController.CreateArticle(Context: TWebContext);
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);
Render(201, 'Article Created');
2014-06-27 15:30:39 +02:00
finally
Article.Free;
end;
end;
2018-12-12 11:00:41 +01:00
procedure TArticlesController.CreateArticles(Context: TWebContext);
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<TArticle>(GetArticlesService.GetAll);
end;
procedure TArticlesController.GetArticlesByDescription;
var
lSearch: String;
begin
try
if random(10) < 2 then
raise EMVCException.Create('ERRORONE!!!');
lSearch := Context.Request.Params['q'];
if lSearch = '' then
Render<TArticle>(GetArticlesService.GetAll)
else
Render<TArticle>(GetArticlesService.GetArticles(lSearch));
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
var
Article: TArticle;
begin
try
Article := GetArticlesService.GetByID(id);
2014-06-27 15:30:39 +02:00
Render(Article);
except
on E: EServiceException do
begin
raise EMVCException.Create(E.Message, '', 0, 404);
end
else
raise;
end;
end;
end.