Important
Coding assignment Messaging application
● We will provide a project template of standard RESTful web service
● The template is using plain SQL strings since wiring up what we have in the backend would be too much effort for this
● Your task is to create database structure and some simple endpoints for an imaginary messaging application
● Imagine, that the service is used by lots of users, so try to optimize database structure for large volumes of data
Requirements:
● User should be able to send messages (like email) to recipients who are other users of the messaging application.
● Message should contain title, message body and timestamp of when it was sent
● Message has a maximum of 5 recipients
● User should have a name
Output
Database
● CREATE TABLE statements for the database structure in the database sql file
● Optionally test data as INSERT statements in the same file
Endpoints implemented in the project template
● Send new message
● Read messages addressed to a specified user
● Statistics endpoint: Top 10 users (by sent message count) sorted by decreasing sent message count for the last 30 days
The cache control directives are being used with CacheControl
class provided by JAX-RS. Currently it is under benchmarking to find which scenario fits for this problem. See more about
This approach allows for concise and expressive code when building responses for RESTful endpoints with proper status code. This API will return in following format:
Status: Message of occurrence
MessageResource
is responsible for handling messages in the system.
This endpoint retrieves messages for a specified user's inbox.
- Path:
/messages/{userId}/inbox
- HTTP Method: GET
- Produces:
application/json
- userId - An integer representing the ID of the user for whom messages are to be retrieved.
Success (HTTP Status: 200 OK)
Returns a list of messages from the inbox of the user. SentAt
in format yyyy-MM-dd
[
{
"title": "Message Title 1",
"body": "Message Body 1",
"sentAt": "2024-02-25",
"senderId": 2
},
{
"title": "Message Title 2",
"body": "Message Body 2",
"sentAt": "2024-02-26",
"senderId": 3
},
// ... other messages
]
Not Found (HTTP Status: 404 Not Found)
Returns an error message when no messages are found for the specified user or User not found from the system.
{
"Not Found: User not found from the system." or "Not Found: No messages found for the user."
}
Internal Server Error (HTTP Status: 500 Internal Server Error)
Returns an error message for any internal server error during message retrieval.
{
"Internal Server Error: Error retrieving messages for the user."
}
curl -X GET http://localhost:8080/messages/1/inbox
This endpoint sends a new message in the system.
- Path:
/messages/send
- HTTP Method: POST
- Consumes:
application/json
- Produces:
application/json
{
"title": "Message Title",
"body": "Message Body",
"senderId": 123,
"receiverIds": [456, 789]
}
-
The
title
,body
,senderId
, andreceiverIds
fields are required and must not be blank or empty. -
receiverIds
should have minimum number of recipients: 1 and maximum number of recipients: 5
HTTP Status: 201 Created
Returns a success message when the message is sent successfully.
{
"Created: The message was sent successfully."
}
HTTP Status: 404 Not Found
An issue occurred, and the record conflicts with the one in the database.
{
"Not Found: Sender not found from the system."
}
HTTP Status: 409 Conflict
An issue occurred, and the record conflicts with the one in the database.
{
"Conflict: The record conflicting with record on the database."
}
Internal Server Error (HTTP Status: 500 Internal Server Error)
Error message when there is a unique constraint violation or SQL error.
{
"Internal Server Error: Unique constraint violation or SQL error"
}
Rollbacks are employed in case of errors to ensure that the database remains in a consistent state.
curl -X POST -H "Content-Type: application/json" -d '{"Invian": "Hello from deadlines", "body": "This should have done before 1 week, but school and other job happened", "senderId": 1, "receiverIds": [456, 789]}' http://localhost:8080/messages/send
StatisticsResource
is responsible for handling statistics related reports.
- Method: GET
- Path:
/statistics/top-users
- Status: 200 OK - Successful retrieval of top users with message count.
- Status: 404 Not Found - No top users found.
- Status: 500 Internal Server Error - Internal server error during processing.
GET /statistics/top-users
- Maven
- Docker
- Docker-compose (installed separately from Docker)
make
Look at the file named Makefile
and run the same commands in the root directory of the project
You should get a congratulation message. If not, try again a few seconds later - the database initialization takes some time. If it still doesn't work, look at the logs.
localhost:3306
Username is root
and password is root_password
The SQL statements in database.sql
are run on every redeploy on the empty database, so evolving the database schema works just like code. Change the statements, redeploy and there it is. No need for ALTER TABLE
statements.
Note that it is not possible to run the REST server outside of Docker.
Java remote debug port is at localhost:5005
Logs can be read with docker logs codingassignment
Logs can be read with docker logs codingassignment-db