mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-16 08:15:53 +01:00
9b70ee40fc
IMVCSerUnSer = interface ['{1ECA942A-E3C4-45DD-9D23-C00363B5E334}'] function SerializeObject(AObject: TObject; AIgnoredProperties: array of string): String; function SerializeObjectStrict(AObject: TObject): String; function SerializeDataSet(ADataSet: TDataSet; AIgnoredFields: array of string): String; function SerializeCollection(AList: TObject; AIgnoredProperties: array of string): String; function SerializeCollectionStrict(AList: TObject): String; procedure DeserializeObject(ASerializedObject: String; AObject: TObject); procedure DeserializeCollection(ASerializedObjectList: string; AList: IMVCList; AClazz: TClass); end;
116 lines
2.7 KiB
ObjectPascal
116 lines
2.7 KiB
ObjectPascal
unit Controllers.Articles;
|
|
|
|
interface
|
|
|
|
uses mvcframework, mvcframework.Commons, Controllers.Base;
|
|
|
|
type
|
|
|
|
[MVCDoc('Resource that manages articles CRUD')]
|
|
[MVCPath('/articles')]
|
|
TArticlesController = class(TBaseController)
|
|
public
|
|
[MVCDoc('Returns the list of articles')]
|
|
[MVCPath]
|
|
[MVCHTTPMethod([httpGET])]
|
|
procedure GetArticles;
|
|
|
|
[MVCDoc('Returns the article with the specified id')]
|
|
[MVCPath('/($id)')]
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
procedure GetArticleByID(id: Integer);
|
|
[MVCDoc('Deletes the article with the specified id')]
|
|
[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"')]
|
|
[MVCPath]
|
|
[MVCHTTPMethod([httpPOST])]
|
|
procedure CreateArticle(Context: TWebContext);
|
|
end;
|
|
|
|
implementation
|
|
|
|
{ TArticlesController }
|
|
|
|
uses Services, BusinessObjects, Commons, mvcframework.Serializer.Intf;
|
|
|
|
procedure TArticlesController.CreateArticle(Context: TWebContext);
|
|
var
|
|
Article: TArticle;
|
|
begin
|
|
Article := Context.Request.BodyAs<TArticle>;
|
|
try
|
|
GetArticlesService.Add(Article);
|
|
Render(201, 'Article Created');
|
|
finally
|
|
Article.Free;
|
|
end;
|
|
end;
|
|
|
|
procedure TArticlesController.DeleteArticleByID(id: Integer);
|
|
var
|
|
Article: TArticle;
|
|
begin
|
|
GetArticlesService.StartTransaction;
|
|
try
|
|
Article := GetArticlesService.GetByID
|
|
(Context.Request.ParamsAsInteger['id']);
|
|
try
|
|
GetArticlesService.Delete(Article);
|
|
finally
|
|
Article.Free;
|
|
end;
|
|
GetArticlesService.Commit;
|
|
except
|
|
GetArticlesService.Rollback;
|
|
raise;
|
|
end;
|
|
end;
|
|
|
|
procedure TArticlesController.GetArticles;
|
|
begin
|
|
Render<TArticle>(GetArticlesService.GetAll);
|
|
end;
|
|
|
|
procedure TArticlesController.UpdateArticleByID(id: Integer);
|
|
var
|
|
Article: TArticle;
|
|
begin
|
|
Article := Context.Request.BodyAs<TArticle>;
|
|
try
|
|
Article.id := Context.Request.ParamsAsInteger['id'];
|
|
GetArticlesService.Update(Article);
|
|
Render(200, 'Article Updated');
|
|
finally
|
|
Article.Free;
|
|
end;
|
|
end;
|
|
|
|
procedure TArticlesController.GetArticleByID(id: Integer);
|
|
var
|
|
Article: TArticle;
|
|
begin
|
|
try
|
|
Article := GetArticlesService.GetByID
|
|
(Context.Request.ParamsAsInteger['id']);
|
|
Render(Article);
|
|
except
|
|
on E: EServiceException do
|
|
begin
|
|
raise EMVCException.Create(E.Message, '', 0, 404);
|
|
end
|
|
else
|
|
raise;
|
|
end;
|
|
end;
|
|
|
|
end.
|