Habit API is a microservice for managing habits and habit entries designed for KaizenLAN profiles. This API allows for creating, retrieving, updating, and deleting habits and habit entries. The application is developed in Java, utilizing the Spring Boot framework, and interacts with a MySQL database using Spring Data JPA.
This section assumes the user is familiar with installing Java 17 and running Maven-based Java applications. It also assumes knowledge of installing and configuring a MySQL database server.
Configure the setup_database.sql file by including your database name.
USE <replace_with_your_database_name>;Then, run the script in a MySQL terminal.
Create the file in the directory ${user.home}/Documents/narlock/secrets/mysql.properties if it does not exist, and include your MySQL credentials so that this API can read them from this properties file.
lmysql.username=<replace_with_your_mysql_username>
lmysql.password=<replace_with_your_mysql_password>
- Using Java 17: After building the application, navigate to the directory of the jar in a terminal and use
java -jar habit-api.jarto run the application. - Using Maven: In a terminal, use mvn
spring-boot:runto run the application.
- Create a habit
- Create an entry for a habit
- Retrieve habits associated to a profile
- Retrieve a habit's streak
- Retrieve date entries for a habit
- Retrieve an entry for a habit by date
- Update a habit's name
- Delete a habit by name and profile ID
POST /habit
Content-Type: application/json
{
"name": "My Habit Name",
"profileId": 1
}Response
HTTP/1.1 201 Created
{
"name": "My Habit Name",
"profileId": 1
}POST /habit-entry
Content-Type: application/json
{
"name": "My Habit Name",
"profileId": 1,
"date": "2024-05-17"
}Response
HTTP/1.1 201 Created
{
"name": "My Habit Name",
"profileId": 1,
"date": "2024-05-17"
}GET /habit?profileId=1
Content-Type: application/jsonResponse
HTTP/1.1 200 OK
[
{
"name": "My Habit Name",
"profileId": 1
},
{
"name": "My Second Habit Name",
"profileId": 1
}
]A habit "streak" represents the number of consecutive days a user with the given profileId has successfully completed a habit.
GET /habit-streak
Content-Type: application/json
{
"name": "My Habit Name",
"profileId": 1
}Response
HTTP/1.1 200 OK
20This example request shows the integer 20 being returned. This is the current streak for this example habit.
GET /habit-entry?name=My Habit Name&profileId=1
Content-Type: application/jsonResponse
HTTP/1.1 200 OK
[
"2024-05-17",
"2024-05-16",
"2024-05-15",
"2024-05-14",
"2024-05-13",
"2024-05-10",
"2024-04-20"
]GET /habit-entry?name=My Habit Name&profileId=1&date=2024-05-17
Content-Type: application/jsonResponse
HTTP/1.1 200 OK
[
"2024-05-17"
]Notably, if there is no entry matching the date parameter, this will return a 404 Not Found
PUT /habit
Content-Type: application/json
{
"oldName": "My Cool Habit",
"newName": "My Awesome Habit",
"profileId": 1
}Response
HTTP/1.1 200 OK
{
"name": "My Awesome Habit",
"profileId": 1
}This operation will delete not only the row in the Habit table matching the given criteria, but will also delete each habit entry matching the given criteria.
DELETE /habit?name=My Habit Name&profileId=1
Content-Type: application/jsonResponse
HTTP/1.1 204 No ContentNote
This section is currently in progress!
