APIs and Microservices Projects #3 - Exercise Tracker
User Stories
- I can create a user by posting form data username to /api/exercise/new-user and returned will be an object with username and _id.
- I can get an array of all users by getting api/exercise/users with the same info as when creating a user.
- I can add an exercise to any user by posting form data userId(_id), description, duration, and optionally date to /api/exercise/add. If no date supplied it will use current date. Returned will be the user object with also with the exercise fields added.
- I can retrieve a full exercise log of any user by getting /api/exercise/log with a parameter of userId(_id). Return will be the user object with added array log and count (total exercise count).
- I can retrieve part of the log of any user by also passing along optional parameters of from & to or limit. (Date format yyyy-mm-dd, limit = int)
요구사항 1. 사용자 생성
작업 순서
학습한 것
- unique validator custom 으로 구현
- 엔드포인트에 validator 와 handler 를 chaining
요구사항 2. 사용자 목록
작업 순서
요구사항 3. 사용자 운동 추가
작업 순서
middleware 문서 참고시 주의할점
- mongoose 는 middleware
- middleware 를 사용할 경우에는 양쪽 엔드 기술문서도 같이 참조.
- 이 경우는 node.js 와 mongodb 문서 참조.
express middle ware 를 활용
- 관심사의 명확한 분리, 책임의 분산 목적
- request 객체를 이용한 데이터 전달
- 현재는 validation error handling 목적으로 middle ware 를 분리
운동 collection 을 분리
- 처음에는 사용자 collection 의 한 property 로 exercise 를 사용
- 운동에만 관심이 있을 경우, 관리가 어렵다
- 사용자와 운동을 따로 설정하고, 운동에 사용자 id를 추가하여 연결
요구사항 4. 사용자별 운동 로그
작업 순서
요구사항 5. 날짜, 갯수 검색
작업순서
구현하면서 알게 된 것들
#. mongodb Query - project
- 어떤 속성을 가리고 어떤 속성을 표시할지 선택
- 연산자와 함께 쓰이면 sub document 까지 제어하는 강력한 도구가 된다.
- mongoose 에서는 select 메서드를 만들어놓아서 아주 조금 편하게 사용가능하다.
#. sub document
- subDocument 를 제어하는 도구들이 많다
- join의 필요성이 많이 줄어든다
- 연산의 복잡성이 있지만, 조금만 학습하면 충분히 단순하게 적용가능하다