Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a CRUD template to the templates #26

Open
burbas opened this issue Feb 6, 2024 · 0 comments
Open

Add a CRUD template to the templates #26

burbas opened this issue Feb 6, 2024 · 0 comments

Comments

@burbas
Copy link
Contributor

burbas commented Feb 6, 2024

I think it would be a lot more intuitive to understand and use nova if it could come with a template with the basic CRUD operations over a given domain model , so that the user could see how can he define the different exposed methods using either querystring or json.
Ideally the template could also contain the code to decode (using a serializer) and fetch some fields of the json
and also the code in order to encode back and send the json response.

The template could include a controller with the methods

  • GET (querystring , responds in json)
  • POST (receives json , responds in json)
  • PUT (receives json , responds in json)
  • DELETE (receives query string , responds with status or whatever)
-module(pet_controller).
-export([get/1, create/1, delete/1,update/1]).

get(Req=#{bindings := #{<<"id">> := Id}})->
   .....
   Pet= fetch_pet_from_somewhere(Id),
   Encode= thoas:encode(Pet),
   {json,200,#{},Encode}.

get(Req=#{parsed_qs:= #{<<"id">> := Id}})->
   .....
   Pet= fetch_pet_from_somewhere(Id),
   Encode= thoas:encode(Pet),
   {json,200,#{},Encode}.

create(Req=#{json := { <<"id">> := Id , <<"hair_color">> := HairColor } })->
    insert_to_db(Req),
     {json,200,#{},<<"created">>}.

update(Req=#{json := { <<"id">> := Id , <<"hair_color">> := HairColor } })->
    update_to_db(Req),
     {json,200,#{},<<"updated">>}.

delete(Req=#{parsed_qs:= { <<"id">> := Id } })->
    delete(Req),
     {json,200,#{},<<"deleted">>}.  
 

The datalayer could just be a mock or another module with undefined methods or just some constant in the method , it doesn't rlly matter but the important part is for the new user to see how he handles json/binding/querystring, serialization,deserialization , json deconstruction (fetching particular fields)

And ideally this template could just be started and checked out with swagger.

P.S The idea came from C# where there are a couple of web api templates pic related
image

Originally posted by @sanzor in novaframework/nova#217

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant