Skip to content

Commit

Permalink
Refactoring, added some data to database
Browse files Browse the repository at this point in the history
  • Loading branch information
massijay committed Sep 11, 2022
1 parent cfebcc3 commit b47bc49
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 60 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ build/
### Mac OS ###
.DS_Store
.idea/uiDesigner.xml

### Vue compiled webapp ###
/src/main/webapp/favicon.ico
/src/main/webapp/assets
/src/main/webapp/index.html
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
# ProgrWeb2021
# TripRecorder
TripRecorder is a diary to save the places you visit during your trips.

The web interface it's written with Vue.js 3 and Bootstrap

The RESTful service is built with Java and runs in a servlet container (e.g. Tomcat)

Data is saved inside a MySQL database

## Trips view

View all your trips filtering them by date in this view and click one of them to see the details

<img src="https://github.com/massijay/ProgrWeb2021/blob/cfebcc3c0df6ce6de58bd7f926a329e8222d9dc4/images/trips-view.png" width="800" alt="Trips View">

## Trip Detail View

View all the details of the selected trip, the trip path inside the map and all the places visited details

<img src="https://github.com/massijay/ProgrWeb2021/blob/cfebcc3c0df6ce6de58bd7f926a329e8222d9dc4/images/trip-detail-view.png" width="800" alt="Trip Detail View">

## Trip Edit View

Add or update a trip, insert all details of it and all the places visited clicking on the map

<img src="https://github.com/massijay/ProgrWeb2021/blob/cfebcc3c0df6ce6de58bd7f926a329e8222d9dc4/images/trip-edit-view.png" width="800" alt="Trip Edit View">
Binary file modified TripRecorder-vue/public/favicon.ico
Binary file not shown.
5 changes: 2 additions & 3 deletions TripRecorder-vue/src/views/TripDetailView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@
</div>
</div>
</div>



<!-- Modal -->
<div class="modal fade" id="deleteTripModal" tabindex="-1" aria-labelledby="deleteTripModal" aria-hidden="true">
<div class="modal-dialog">
Expand Down Expand Up @@ -102,7 +101,7 @@ const {
} = useTripMap();
onMounted(() => {
initMap(true);
initMap(false);
getAndDrawTripOnMap(route.params.trip_id);
});
Expand Down
25 changes: 25 additions & 0 deletions init/02-data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
INSERT INTO `users` (`id`, `username`, `password`, `email`, `created_at`, `updated_at`) VALUES
(1, 'massimiliano', '1000:O/zy5Tlzn/QI4J4xl6STgQ==:Qnuz5fK8hy//vesYFA6Gb+C7O8pscjDO4HYb4RrsbfkEdUlIUGiemX6I6jbnF08Fli0E/V5rGC6qwIMqamhh4w==', '[email protected]', '2022-09-11 19:15:56', '2022-09-11 19:15:56');

INSERT INTO `sessions` (`token`, `user_id`, `expire_at`) VALUES
('75aee90a-d2d4-4feb-8cdc-f6549a800477', 1, '2022-09-11 20:25:32');

INSERT INTO `trips` (`id`, `user_id`, `trip_name`, `trip_date`, `transport_type`, `notes`) VALUES
(1, 1, 'Calabria 2022', '2022-08-03 22:00:00', 'Treno', 'Viaggio in Calabria dell\'estate 2022 facendo tappa a Venezia, Bologna e Roma'),
(2, 1, 'Premantura 2022', '2022-08-14 22:00:00', 'Macchina', 'Viaggio di andata per il campeggio a Premantura a ferragosto 2022');

INSERT INTO `geopoints` (`id`, `trip_id`, `latitude`, `longitude`, `recorded_at`, `label`) VALUES
(2, 1, 45.817315086704404, 13.48576681068872, '2022-08-04 05:50:00', 'Stazione Trieste Airport'),
(3, 1, 45.43700838601308, 12.335684577874144, '2022-08-04 08:30:00', 'Venezia'),
(4, 1, 44.50238248659188, 11.33318206479087, '2022-08-04 10:30:00', 'Bologna'),
(5, 1, 41.885921309264944, 12.4985174068097, '2022-08-04 18:30:00', 'Roma'),
(6, 1, 40.851215786677834, 14.195905160482196, '2022-08-04 20:00:00', ''),
(7, 1, 40.67647235480396, 14.74522152756441, '2022-08-04 21:30:00', ''),
(8, 1, 40.067562749450005, 15.623343180565398, '2022-08-05 00:30:00', ''),
(9, 1, 39.121537775852175, 16.085553483612287, '2022-08-05 02:30:00', ''),
(10, 1, 38.706946223127815, 16.136953987899915, '2022-08-05 04:20:00', ''),
(11, 1, 38.09565997512077, 15.648062421196729, '2022-08-05 06:00:00', 'Reggio Calabria'),
(12, 2, 45.65052839048781, 13.776364980619253, '2022-08-15 05:30:00', 'Trieste'),
(13, 2, 45.54242700317452, 13.72280663306619, '2022-08-15 07:30:00', ''),
(14, 2, 44.869982881657535, 13.846991389822938, '2022-08-15 09:50:00', 'Pola'),
(15, 2, 44.8008396264392, 13.910162774460872, '2022-08-15 11:00:00', 'Premantura');
3 changes: 1 addition & 2 deletions src/main/java/com/mcris/triprecorder/filters/AuthFilter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mcris.triprecorder.filters;

import com.mcris.triprecorder.models.SessionSecurityContext;
import com.mcris.triprecorder.models.utils.SessionSecurityContext;
import com.mcris.triprecorder.models.entities.Session;
import com.mcris.triprecorder.providers.DBProvider;

Expand Down Expand Up @@ -47,7 +47,6 @@ public void filter(ContainerRequestContext containerRequestContext) {
containerRequestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
return;
}
// TODO: magic numbers
Instant instant = Instant.now().plusSeconds(1800);
session.setExpireAt(Timestamp.from(instant));
session = DBProvider.getInstance().updateSession(session);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mcris.triprecorder.models;
package com.mcris.triprecorder.models.utils;

import com.mcris.triprecorder.providers.DBProvider;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.mcris.triprecorder.models.utils;

public class ErrorMessage {
public static final int USERNAME_ALREADY_EXISTS = 1;
public static final int EMAIL_ALREADY_EXISTS = 2;
public static final int INVALID_EMAIL = 3;
public String message;
public int errorCode;

public ErrorMessage(String message, int errorCode) {
this.message = message;
this.errorCode = errorCode;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public int getErrorCode() {
return errorCode;
}

public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mcris.triprecorder.models;
package com.mcris.triprecorder.models.utils;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mcris.triprecorder.models;
package com.mcris.triprecorder.models.utils;

import com.mcris.triprecorder.models.entities.Session;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mcris.triprecorder.models;
package com.mcris.triprecorder.models.utils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import java.util.stream.Collectors;

public class DBProvider {

//TODO: check transaction and rollback if needed
private static DBProvider instance = null;

public static DBProvider getInstance() {
Expand Down
23 changes: 7 additions & 16 deletions src/main/java/com/mcris/triprecorder/resources/AuthResources.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.mcris.triprecorder.resources;

import com.mcris.triprecorder.models.PasswordUtils;
import com.mcris.triprecorder.models.SessionSecurityContext;
import com.mcris.triprecorder.models.entities.Session;
import com.mcris.triprecorder.models.entities.User;
import com.mcris.triprecorder.models.utils.ErrorMessage;
import com.mcris.triprecorder.models.utils.PasswordUtils;
import com.mcris.triprecorder.models.utils.SessionSecurityContext;
import com.mcris.triprecorder.providers.DBProvider;
import org.glassfish.jersey.server.ContainerRequest;

Expand All @@ -20,6 +21,7 @@
@Path("/auth")
public class AuthResources {
static final Pattern emailPattern = Pattern.compile("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$", Pattern.CASE_INSENSITIVE);

@POST
@Path("login")
@Consumes(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -47,24 +49,18 @@ public Response register(User user, @Context ContainerRequest containerRequest)
}
Matcher matcher = emailPattern.matcher(user.getEmail());
if (!matcher.find()) {
ErrorMessage em = new ErrorMessage();
em.message = "Invalid email address";
em.errorCode = 3;
ErrorMessage em = new ErrorMessage("Invalid email address", ErrorMessage.INVALID_EMAIL);
return Response.status(422).entity(em).build(); // UNPROCESSABLE ENTITY
}
user.setId(0);
boolean usernameAlreadyExist = DBProvider.getInstance().getUserByUsername(user.getUsername()) != null;
if (usernameAlreadyExist) {
ErrorMessage em = new ErrorMessage();
em.message = "An user with this username already exist";
em.errorCode = 1;
ErrorMessage em = new ErrorMessage("An user with this username already exist", ErrorMessage.USERNAME_ALREADY_EXISTS);
return Response.status(422).entity(em).build(); // UNPROCESSABLE ENTITY
}
boolean emailAlreadyExist = DBProvider.getInstance().getUserByEmail(user.getEmail()) != null;
if (emailAlreadyExist) {
ErrorMessage em = new ErrorMessage();
em.message = "An user with this email already exist";
em.errorCode = 2;
ErrorMessage em = new ErrorMessage("An user with this email already exist", ErrorMessage.EMAIL_ALREADY_EXISTS);
return Response.status(422).entity(em).build(); // UNPROCESSABLE ENTITY
}
user.setPassword(PasswordUtils.hashAndSaltPassword(user.getPassword()));
Expand All @@ -82,8 +78,3 @@ public Response logout(@Context ContainerRequest containerRequest) {
}
}

// TODO: move this out and improve?
class ErrorMessage {
public String message;
public int errorCode;
}
22 changes: 0 additions & 22 deletions src/main/java/com/mcris/triprecorder/resources/TripsResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public Response getUserTrips(@QueryParam("date") String date, @Context Container
Collection<Trip> trips = DBProvider.getInstance().getUserTrips(user, localDate);
return Response.ok(trips).build();
} catch (DateTimeParseException ex) {
// TODO: Create enum with extra HTTP codes
return Response.status(422).build(); // UNPROCESSABLE ENTITY
}
}
Expand Down Expand Up @@ -64,8 +63,6 @@ public Response postTrip(Trip trip, @Context ContainerRequest containerRequest)
return Response.ok(newTrip).build();
}

// TODO: fare PATCH e PUT di Trip

@PATCH
@Path("{trip_id}")
@Consumes(MediaType.APPLICATION_JSON)
Expand All @@ -84,25 +81,6 @@ public Response patchTrip(Trip trip, @PathParam("trip_id") int tripId, @Context
return Response.ok(updatedTrip).build();
}

// @PUT
// @Path("{trip_id}")
// @Consumes(MediaType.APPLICATION_JSON)
// @Produces(MediaType.APPLICATION_JSON)
// public Response putTrip(Trip trip, @PathParam("trip_id") int tripId, @Context ContainerRequest containerRequest) {
// if (trip == null || tripId <= 0) {
// return Response.status(422).build(); // UNPROCESSABLE ENTITY
// }
// User user = (User) containerRequest.getSecurityContext().getUserPrincipal();
// if (!(trip.getUserId() == 0 || trip.getUserId() == user.getId())) {
// return Response.status(422).build(); // UNPROCESSABLE ENTITY
// }
// boolean deleteResult = DBProvider.
// trip.setUserId(user.getId());
// trip.setId(tripId);
// Trip updatedTrip = DBProvider.getInstance().addOrUpdateTrip(trip);
// return Response.ok(updatedTrip).build();
// }

@DELETE
@Path("{trip_id}")
public Response deleteTrip(@PathParam("trip_id") int tripId, @Context ContainerRequest containerRequest) {
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/com/mcris/triprecorder/resources/UsersResource.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.mcris.triprecorder.resources;

import com.mcris.triprecorder.models.PasswordUtils;
import com.mcris.triprecorder.models.entities.User;
import com.mcris.triprecorder.models.utils.ErrorMessage;
import com.mcris.triprecorder.models.utils.PasswordUtils;
import com.mcris.triprecorder.providers.DBProvider;
import org.glassfish.jersey.server.ContainerRequest;

Expand Down Expand Up @@ -31,24 +32,18 @@ public Response patchUser(User newUser, @Context ContainerRequest containerReque
}
Matcher matcher = emailPattern.matcher(newUser.getEmail());
if (!matcher.find()) {
ErrorMessage em = new ErrorMessage();
em.message = "Invalid email address";
em.errorCode = 3;
ErrorMessage em = new ErrorMessage("Invalid email address", ErrorMessage.INVALID_EMAIL);
return Response.status(422).entity(em).build(); // UNPROCESSABLE ENTITY
}
User existingUser = (User) containerRequest.getSecurityContext().getUserPrincipal();
User found = DBProvider.getInstance().getUserByUsername(newUser.getUsername());
if (found != null && found.getId() != existingUser.getId()) {
ErrorMessage em = new ErrorMessage();
em.message = "An user with this username already exist";
em.errorCode = 1;
ErrorMessage em = new ErrorMessage("An user with this username already exist", ErrorMessage.USERNAME_ALREADY_EXISTS);
return Response.status(422).entity(em).build(); // UNPROCESSABLE ENTITY
}
found = DBProvider.getInstance().getUserByEmail(newUser.getEmail());
if (found != null && found.getId() != existingUser.getId()) {
ErrorMessage em = new ErrorMessage();
em.message = "An user with this email already exist";
em.errorCode = 2;
ErrorMessage em = new ErrorMessage("An user with this email already exist", ErrorMessage.EMAIL_ALREADY_EXISTS);
return Response.status(422).entity(em).build(); // UNPROCESSABLE ENTITY
}
if (newUser.getUsername() != null) {
Expand Down

0 comments on commit b47bc49

Please sign in to comment.