-
Notifications
You must be signed in to change notification settings - Fork 0
Code snippets
ArmandasRokas edited this page Apr 27, 2019
·
1 revision
/**
* Creates a user in the data layer.
* @param user an instance of the User class with mandatory fields id, userName, email, and password
* @throws UserMissingLocationException if user object is missing valid location object (only unique location id is required)
* @throws UserIdDuplicationException if user has id already existing in the data layer
* @throws UserNameDuplicationException if user has userName already existing in the data layer
* @throws UserEmailDuplicationException if user has email already existing in the data layer
* @throws CreateUserException if some other unexpected error occurred
*/
void createUser(User user) throws UserIdDuplicationException,
UserNameDuplicationException, UserEmailDuplicationException,
CreateUserException, UserMissingLocationException ;INSERT INTO user (id, user_name, email, password) VALUES ( ? , ? , ? , ? )
INSERT INTO location (id, street_name, street_number, city, country, spatial_point) VALUES ( ? , ? , ? , ? , ? , ST_GeomFromText( ? , ? ))
INSERT INTO location_user ( location_id, user_id ) VALUES ( ? , ? )/**
* Retrieves a user from the data layer.
* @param userName is the user name of an existing user in the data layer
* @return User object retrieved from the data layer
* @throws UserNotFoundException if the user with that userName is not found in the data layer
* @throws UserRepositoryException if other exceptions occurred
*/
User getUser(String userName) throws UserNotFoundException, UserRepositoryException;SELECT * FROM user WHERE user.user_name = ?
SELECT id, street_name, street_number, city, country, ST_X(spatial_point) AS X, ST_Y(spatial_point) AS Y
FROM location JOIN location_user
ON location_user.location_id = location.id
WHERE location_user.user_id = ? "
/**
* Updates an existing user in the data layer.
* @param updatedUser the updated User object
* @throws UserNameDuplicationException if the new userName is already taken by another user
* @throws UserEmailDuplicationException if the new email is already taken by another user
* @throws UserNotFoundException if the id of updatedUser is not found in the data layer
* @throws UpdateUserException if other exceptions occurred
*/
void updateUser(User updatedUser) throws UserNameDuplicationException, UserEmailDuplicationException, UpdateUserException, UserNotFoundException;UPDATE user SET user_name = ?, email = ?, password = ? WHERE id = ?
UPDATE location SET street_name = ? , street_number = ? , city = ?,
country = ?, spatial_point = ST_GeomFromText( ? , ? )
WHERE location.id = ?;/**
* Creates a run in the data layer for an existing route and a user.
* @param run the new run object with unique id and attached existing route, the route object as minimum needs the correct route id
* @param participantId is the id of the user, if the id is not existing in the data layer UnknownUserException exception is thrown
* @throws RunIdDuplicationException if there is a run with the same id in the data layer
* @throws UnknownRouteException if the route by id is not an existing route in the data layer
* @throws UnknownUserException if the participantId not the id of an existing user in the data layer
* @throws CreateRunException other not predefined exceptions are wrapped in this exception
* @throws RunValidationException if run object is not constructed properly
* @throws MaxParticipansReachedException if route has reached max number of participants
*/
void createRun(Run run, String participantId) throws RunIdDuplicationException,
UnknownRouteException, UnknownUserException, CreateRunException, RunValidationException;INSERT INTO run VALUES ( ‘run_id’ , ‘route_id’ , ‘participant_id’ )/** Adds a checkpoint to the run if the user is close to
* one of the waypoints of the route.
*
* @param runId the id of the current run, which a user is running
* @param currentX the latitude value of the user's current coordinate
* @param currentY the longitude value of the user's current coordinate
* @param precision describes how close a user should be to the waypoint in order to mark a waypoint as checked
* @throws CheckpointException if there is occurred any error inserting a checkpoint
*/
void addCheckpointIfValid(String runId, double currentX, double currentY, int precision) throws CheckpointException;INSERT INTO checkpoint (run_id, waypoint_index, visited_timestamp)
SELECT run.id, `index`, now()
FROM waypoint
JOIN run ON waypoint.route_id = run.route_id
WHERE run.id = ? AND
ST_Distance(spatial_point, ST_GeomFromText( ? )) <= ?