Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ SingleEventResponse createEvent(CreateEventRequest request, JWTData userData)

SingleEventResponse getSingleEvent(int eventId, JWTData userData);

GetEventsResponse getEvents(List<Integer> event, JWTData userData);
GetEventsResponse getEventsByIds(List<Integer> event, JWTData userData);

GetEventsResponse getEvents(JWTData userData);

GetEventsResponse getEventsSignedUp(GetUserEventsRequest request, JWTData userData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@
/** A class to represent the details of an event. */
public class EventDetails extends ApiDto {
private String description;
private String privateDescription;
private String location;
private Timestamp start;
private Timestamp end;

public EventDetails(String description, String location, Timestamp start, Timestamp end) {
public EventDetails(
String description,
String privateDescription,
String location,
Timestamp start,
Timestamp end) {
this.description = description;
this.privateDescription = privateDescription;
this.location = location;
this.start = start;
this.end = end;
Expand All @@ -41,6 +48,24 @@ public void setDescription(String description) {
this.description = description;
}

/**
* Gets the event's private description.
*
* @return the event's private description
*/
public String getPrivateDescription() {
return privateDescription;
}

/**
* Sets the given private description as the description of the event
*
* @param privateDescription the private description to be set
*/
public void setPrivateDescription(String privateDescription) {
this.privateDescription = privateDescription;
}

/**
* Gets the location of the event.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public GetUserEventsRequest(
this.count = count;
}

public GetUserEventsRequest() {
this.endDate = Optional.empty();
this.startDate = Optional.empty();
this.count = Optional.empty();
}

/**
* Gets the end date of the event.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import java.sql.Timestamp;
import java.util.List;
import java.util.Optional;

public class EventsRouter implements IRouter {
Expand Down Expand Up @@ -92,11 +91,9 @@ private void registerGetEventRSVPs(Router router) {
}

private void handleGetEvents(RoutingContext ctx) {

List<Integer> intIds = getMultipleQueryParams(ctx, "ids", str -> Integer.parseInt(str));
JWTData userData = ctx.get("jwt_data");

GetEventsResponse response = processor.getEvents(intIds, userData);
GetEventsResponse response = processor.getEvents(userData);

end(ctx.response(), 200, JsonObject.mapFrom(response).encode());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE events
ADD COLUMN private_description TEXT DEFAULT NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private void updateSystemProperties() {

/** Connect to the database and create a DSLContext so jOOQ can interact with it. */
private void createDatabaseConnection() throws ClassNotFoundException {

// Load configuration from db.properties file
String databaseDriver = PropertiesLoader.loadProperty("database_driver");
String databaseUrl = PropertiesLoader.loadProperty("database_url");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public UserInformation getUserInformation(Users user) {
AddressData locationData = extractAddressDataFromUser(user);
PrivilegeLevel privilegeLevel = user.getPrivilegeLevel();

return new UserInformation(mainContact, additionalContacts, children, locationData, privilegeLevel);
return new UserInformation(
mainContact, additionalContacts, children, locationData, privilegeLevel);
}

/**
Expand Down Expand Up @@ -180,9 +181,9 @@ public UsersRecord createNewUser(NewUserRequest request) {
public void addToBlackList(String signature) {
Timestamp expirationTimestamp = Timestamp.from(Instant.now().plusMillis(msRefreshExpiration));
db.insertInto(Tables.BLACKLISTED_REFRESHES)
.values(signature, expirationTimestamp)
.onDuplicateKeyIgnore()
.execute();
.values(signature, expirationTimestamp)
.onDuplicateKeyIgnore()
.execute();
}

/** Given a JWT signature return true if it is stored in the BLACKLISTED_REFRESHES table. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.codeforcommunity.processor;

import static org.jooq.generated.Tables.ANNOUNCEMENTS;
import static org.jooq.generated.Tables.CHILDREN;
import static org.jooq.generated.Tables.CONTACTS;
import static org.jooq.generated.Tables.EVENTS;
import static org.jooq.generated.Tables.EVENT_REGISTRATIONS;
import static org.jooq.generated.Tables.USERS;

import com.codeforcommunity.api.IEventsProcessor;
import com.codeforcommunity.auth.JWTData;
import com.codeforcommunity.dataaccess.EventDatabaseOperations;
Expand All @@ -19,14 +26,6 @@
import com.codeforcommunity.exceptions.InvalidEventCapacityException;
import com.codeforcommunity.exceptions.S3FailedUploadException;
import com.codeforcommunity.requester.S3Requester;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.SelectConditionStep;
import org.jooq.SelectSeekStep1;
import org.jooq.generated.tables.pojos.Events;
import org.jooq.generated.tables.records.EventsRecord;
import org.jooq.impl.DSL;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
Expand All @@ -35,13 +34,13 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.jooq.generated.Tables.ANNOUNCEMENTS;
import static org.jooq.generated.Tables.CHILDREN;
import static org.jooq.generated.Tables.CONTACTS;
import static org.jooq.generated.Tables.EVENTS;
import static org.jooq.generated.Tables.EVENT_REGISTRATIONS;
import static org.jooq.generated.Tables.USERS;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.SelectConditionStep;
import org.jooq.SelectSeekStep1;
import org.jooq.generated.tables.pojos.Events;
import org.jooq.generated.tables.records.EventsRecord;
import org.jooq.impl.DSL;

public class EventsProcessorImpl implements IEventsProcessor {

Expand Down Expand Up @@ -88,11 +87,23 @@ public SingleEventResponse getSingleEvent(int eventId, JWTData userData) {
}

@Override
public GetEventsResponse getEvents(List<Integer> eventIds, JWTData userData) {
public GetEventsResponse getEventsByIds(List<Integer> eventIds, JWTData userData) {
List<Events> e = db.selectFrom(EVENTS).where(EVENTS.ID.in(eventIds)).fetchInto(Events.class);
return new GetEventsResponse(listOfEventsToListOfSingleEventResponse(e, userData), e.size());
}

@Override
public GetEventsResponse getEvents(JWTData userData) {
Timestamp startDate =
Timestamp.from(Instant.now().minus(registerLeniencyHours, ChronoUnit.HOURS));
List<Events> e =
db.selectFrom(EVENTS)
.where(EVENTS.START_TIME.greaterOrEqual(startDate))
.orderBy(EVENTS.START_TIME.asc())
.fetchInto(Events.class);
return new GetEventsResponse(listOfEventsToListOfSingleEventResponse(e, userData), e.size());
}

@Override
public GetEventsResponse getEventsSignedUp(GetUserEventsRequest request, JWTData userData) {

Expand Down Expand Up @@ -191,6 +202,9 @@ public SingleEventResponse modifyEvent(
if (details.getDescription() != null) {
record.setDescription(details.getDescription());
}
if (details.getPrivateDescription() != null) {
record.setPrivateDescription(details.getPrivateDescription());
Comment thread
ja0911 marked this conversation as resolved.
}
if (details.getLocation() != null) {
record.setLocation(details.getLocation());
}
Expand Down Expand Up @@ -230,6 +244,16 @@ public EventRegistrations getEventRegisteredUsers(int eventId, JWTData userData)
throw new AdminOnlyRouteException();
}

return this.getEventRegisteredUsers(eventId);
}

/**
* Returns an EventRegistrations object containing all of the users registered for this event
*
* @param eventId id of the event
* @return an EventRegistrations object containing all of the users registered for this event
*/
private EventRegistrations getEventRegisteredUsers(int eventId) {
if (!db.fetchExists(EVENTS.where(EVENTS.ID.eq(eventId)))) {
throw new EventDoesNotExistException(eventId);
}
Expand Down Expand Up @@ -352,36 +376,38 @@ private List<SingleEventResponse> listOfEventsToListOfSingleEventResponse(
List<Events> events, JWTData userData) {
Map<Integer, Integer> ticketCounts = getTicketCounts(events, userData.getUserId());
return events.stream()
.map(
event -> {
EventDetails details =
new EventDetails(
event.getDescription(),
event.getLocation(),
event.getStartTime(),
event.getEndTime());
return new SingleEventResponse(
event.getId(),
event.getTitle(),
eventDatabaseOperations.getSpotsLeft(event.getId()),
event.getCapacity(),
event.getThumbnail(),
details,
ticketCounts.getOrDefault(event.getId(), 0),
canUserRegister(event, userData),
event.getPrice());
})
.map(event -> eventPojoToResponse(event, userData))
.collect(Collectors.toList());
}

/** Takes a database representation of a single event and returns the dto representation. */
private SingleEventResponse eventPojoToResponse(Events event, JWTData userData) {

// getting all of the users registered for this event
EventRegistrations registrations = this.getEventRegisteredUsers(event.getId());
boolean userRegisteredForEvent = false;
for (Registration reg : registrations.getRegistrations()) {
if (reg.getUserId() == userData.getUserId()) {
userRegisteredForEvent = true;
break;
}
}

// hide the private description if the user isn't registered and they're not an admin
if (!userRegisteredForEvent && userData.getPrivilegeLevel() != PrivilegeLevel.ADMIN) {
event.setPrivateDescription(null);
}

int ticketsBought =
getTicketCounts(Arrays.asList(event), userData.getUserId()).getOrDefault(event.getId(), 0);

EventDetails details =
new EventDetails(
event.getDescription(), event.getLocation(), event.getStartTime(), event.getEndTime());
event.getDescription(),
event.getPrivateDescription(),
Comment thread
ja0911 marked this conversation as resolved.
event.getLocation(),
event.getStartTime(),
event.getEndTime());
return new SingleEventResponse(
event.getId(),
event.getTitle(),
Expand All @@ -399,6 +425,7 @@ private EventsRecord eventRequestToRecord(CreateEventRequest request) {
EventsRecord newRecord = db.newRecord(EVENTS);
newRecord.setTitle(request.getTitle());
newRecord.setDescription(request.getDetails().getDescription());
newRecord.setPrivateDescription(request.getDetails().getPrivateDescription());
newRecord.setThumbnail(request.getThumbnail());
newRecord.setCapacity(request.getCapacity());
newRecord.setLocation(request.getDetails().getLocation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ private List<PublicSingleEventResponse> listOfEventsToListOfPublicSingleEventRes
EventDetails details =
new EventDetails(
event.getDescription(),
null,
Comment thread
ja0911 marked this conversation as resolved.
event.getLocation(),
event.getStartTime(),
event.getEndTime());
Expand Down
Loading