Skip to content

Week 9 Exercise Ready for Review #46

@ArchILLtect

Description

@ArchILLtect

@pawaitemadisoncollege - week 9 ready for review

This submission completes the Week 9 Exercise: Service using DAO (and Extra Challenge).

References PR #47

Main

Rather than creating a redundant new service, I expanded and clarified my existing ChallengeService, which already performs the same function described in the exercise:

No ID → returns all (findAll)

ID provided → returns one (findById) *Returns an entity as required for the challenge

This approach keeps my project consistent and avoids unnecessary duplication, while still fully demonstrating the DAO → Service pattern and the “extra challenge” requirement.

Supporting components

ChallengeDao – DAO providing persistence operations

ChallengeService – wraps the DAO for application/business logic

Week9ChallengeService – lightweight delegate demonstrating clear exercise intent

Week9ChallengeServiceTest – validates delegation logic

Files to Review

Type File Purpose
DAO src/main/java/.../persistence/ChallengeDao.java Data access logic
Service src/main/java/.../service/ChallengeService.java Uses DAO, provides create/update/getAll/getById
Week 9 Delegate src/main/java/.../service/Week9ChallengeService.java Simplified service fulfilling exercise requirements
Tests src/test/java/.../service/ChallengeServiceTest.java Validates core service logic
Tests src/test/java/.../service/Week9ChallengeServiceTest.java Verifies proper delegation for Week 9 exercise
DAO Tests src/test/java/.../persistence/ChallengeDaoTest.java Ensures persistence layer integrity
Test Base src/test/java/.../persistence/DaoTestBase.java Handles H2 DB reset between tests

Rationale

I noticed that my existing ChallengeService already aligns with the intent of the Week 9 exercise. It consumes a DAO, abstracts persistence concerns, and returns either a full collection or a single entity depending on context.

To make this connection explicit, I added a small Week9ChallengeService layer that delegates to ChallengeService, making the “DAO → Service → Controller” relationship perfectly clear for grading purposes.

This also avoids introducing much dead code (I can just remove the week9 related class/test) — the pattern is real, used, and directly beneficial to the overall app structure.

If expanded externally, I would expose the same logic via a simple REST endpoint:

@WebServlet(urlPatterns = {"/week9"})
public class Week9ChallengeServlet extends HttpServlet {

    private final ChallengeService service = new ChallengeService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        resp.setContentType("text/plain;charset=UTF-8");

        String idParam = req.getParameter("id");

        if (idParam != null) {
            try {
                Long id = Long.parseLong(idParam);
                Optional<Challenge> chOpt = service.getById(id);
                if (chOpt.isPresent()) {
                    Challenge ch = chOpt.get();
                    resp.getWriter().printf("Challenge #%d: %s (%s)%nBlurb: %s%n",
                            ch.getId(), ch.getTitle(), ch.getDifficulty(), ch.getBlurb());
                } else {
                    resp.getWriter().println("No challenge found with id=" + id);
                }
            } catch (NumberFormatException e) {
                resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid id format");
            }
            return;
        }

        // No id → return all
        List<Challenge> all = service.listChallenges(null);
        resp.getWriter().println("=== All Challenges ===");
        for (Challenge c : all) {
            resp.getWriter().printf("%d. %s (%s)%n", c.getId(), c.getTitle(), c.getDifficulty());
        }
    }
}

This would expose /api/challenges for all challenges and /api/challenges?id=42 for one — as an entity completing the “extra challenge” at a web-service level.

Screenshots

Browser:

Week 9 getAll screenshot
Week 9 getById screenshot

Tests:

Image Image Image

Reflection

Key Learning Points / Takeaways:

  • Learned to clearly separate DAO vs. Service responsibilities.
  • Reinforced understanding of dependency injection and mock-based testing.
  • Gained confidence in using Mockito for controlled delegation testing.
  • Learned to reuse existing logic instead of duplicating functionality.

Challenges:

  • Ensuring SessionFactoryProvider and H2 initialization worked consistently in CI.
  • Understanding when and how to mock vs. when to test with a real DB.
  • Deciding whether to extend or rebuild existing functionality for this exercise.

Problems Solved / Resources Used:

  • Solved CI initialization issues by adding a resilient SessionFactoryProvider fallback for test runtime.
  • Used official Mockito docs, class examples from previous weeks, and my own DAO integration tests as reference points.
  • Refined tests to cover both CRUD and delegation logic cleanly.

Summary Statement

All service tests are passing, and the new Week9ChallengeService fulfills both the base and the “extra challenge” requirements.
This solution is functionally complete, test-verified, and aligned with Week 9 objectives while remaining consistent with the app’s real structure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api:externalWork related to external/public API selection, integration, and resilience.area:persistenceEntities, DAOs, JPA/Hibernate mappings, and database concerns.area:serviceBusiness logic and orchestration (e.g., DrillService, ChallengeRunService).area:webControllers, JSP/JSTL, view models, and UI wiring.checkpointUse for Project Checkpoints. Include acceptance evidence (tests, screenshots) when used.priority:P1-highHigh importance; schedule next.project:mvpUse for all issues/PRs that belong to the MVP release. Will auto-add labeled items to the board.status:needs-reviewReady for code review or product approval.status:triageNewly filed or uncategorized. Needs initial review, labeling, and priority assignment.

    Projects

    Status

    In Progress

    Status

    In Progress

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions