2014-06-27 15:30:39 +02:00
|
|
|
unit Controllers.Articles;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
2017-01-18 21:53:53 +01:00
|
|
|
uses mvcframework, mvcframework.Commons, 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
|
|
|
|
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-11-24 20:07:45 +01:00
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
procedure GetArticleByID(id: Integer);
|
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])]
|
2014-06-27 16:38:49 +02:00
|
|
|
procedure CreateArticle(Context: TWebContext);
|
2014-06-27 15:30:39 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{ TArticlesController }
|
|
|
|
|
2017-02-08 18:29:52 +01:00
|
|
|
uses Services, BusinessObjects, Commons, mvcframework.Serializer.Intf;
|
2014-06-27 15:30:39 +02:00
|
|
|
|
2014-06-27 16:38:49 +02:00
|
|
|
procedure TArticlesController.CreateArticle(Context: TWebContext);
|
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);
|
2014-06-27 16:38:49 +02:00
|
|
|
Render(201, 'Article Created');
|
2014-06-27 15:30:39 +02:00
|
|
|
finally
|
|
|
|
Article.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
|
2016-11-24 20:07:45 +01:00
|
|
|
Article := GetArticlesService.GetByID
|
|
|
|
(Context.Request.ParamsAsInteger['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
|
|
|
|
Render<TArticle>(GetArticlesService.GetAll);
|
|
|
|
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
|
2016-06-22 17:49:16 +02:00
|
|
|
Article.id := Context.Request.ParamsAsInteger['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
|
|
|
var
|
|
|
|
Article: TArticle;
|
|
|
|
begin
|
|
|
|
try
|
2016-11-24 20:07:45 +01:00
|
|
|
Article := GetArticlesService.GetByID
|
|
|
|
(Context.Request.ParamsAsInteger['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.
|