center-for-threat-informed-defense / attack-workbench-rest-api

An application allowing users to explore, create, annotate, and share extensions of the MITRE ATT&CK® knowledge base. This repository contains the REST API service for storing, querying, and editing ATT&CK objects.
https://ctid.mitre-engenuity.org/
Apache License 2.0
42 stars 22 forks source link

Separate Database Logic From Service Layer (Proof of Concept) #281

Open seansica opened 1 year ago

seansica commented 1 year ago

Objective:

Separate the database logic from the service layer to improve code modularity, maintainability, and facilitate testing.

Background:

Currently, our Express.js API directly performs database operations in the service layer. This intertwined structure can become a maintenance challenge and hinders the clarity and separability of concerns in our codebase.

Proposed Changes:

For each of the service layer modules, the following should be completed:

  1. Introduce a Repository Layer

    • Create a new layer (Repository or DAO) specifically for handling raw database operations.
    • Define methods in the repository layer like findWithAggregation, findMatrixById, and findOneMatrixById.
  2. Refactor Service Layer

    • The service layer should call the newly defined repository methods for database operations.
    • Retain business logic, such as handling transactions and complex computations, in the service layer.
  3. Unit Testing

    • Adjust unit tests (if any) to align with the new structure.
    • Mock the repository methods when testing the service layer.
  4. Update Controllers

    • Ensure controllers remain thin and only handle HTTP requests, input validation, and returning responses.
  5. Overhaul Error Handling

    • Implement and use custom exception classes hosted by the app/exceptions module.

Tasks