Skip to content

Instantly share code, notes, and snippets.

@lancetw
Created July 16, 2021 08:54
Show Gist options
  • Save lancetw/a5b2dffc4fa181a694e39a4dc42c3793 to your computer and use it in GitHub Desktop.
Save lancetw/a5b2dffc4fa181a694e39a4dc42c3793 to your computer and use it in GitHub Desktop.

News APIs:

  • Fetch: [GET] -> /news/:id
  • List: [GET] -> /news/list
  • Add: [POST] -> /news
  • Update: [POST] -> /news/:id
  • Remove: [DELETE] -> /news/:id

WEB Backend Components:

 Model / View / Controller  Model / Template (Django) / View (Django)


Service Layer (Data CRUD):

Data Access Object, DAO Serializer and Model (Django)


A DRF View (Controller) without Serializer Example:

# [*A]: could be Decorator
# [*B]: could be Django Serializer

class ListNews(APIView):

  '''
  List: [GET] -> /news/list
  '''
  def get(self, request, format=None):

    fields = ['cursor', 'key']

    if not Validator.validate(request):  # [*A]
      return Response(Validator.errors, status=status.HTTP_400_BAD_REQUEST)

    querySet = DAO.loadAll(filterBy(fields)) # [*B]

    results = maskData(querySet) # [*B]
    
    return Response(results)

  '''
  Add: [POST] -> /news
  '''
  def post(self, request, format=None):
    # id = DAO.save(data)
    pass
        
class NewsDetail(APIView):

  '''
  Fetch: [GET] -> /news/:id
  '''
  def get(self, request, format=None):

    fields = ['key']

    if not Validator.validate(request, fields): # [*A]
      return Response(Validator.errors, status=status.HTTP_400_BAD_REQUEST)

    querySet = DAO.load(filterBy(request, fields)) # [*B]

    item = maskData(querySet) # [*B]

    if not data:
      return Response(None, status=status.HTTP_404_NOT_FOUND)
    
    return Response(item)

  '''
  Update: [POST] -> /news/:id
  '''
  def post(self, request, format=None):
    # DAO.saveOrUpdate(id, data)
    pass

  '''
  Remove: [DELETE] -> /news/:id 
  '''
  def delete(self, request, format=None):
    # DAO.destroy(id)
    pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment