Skip to content

Commit

Permalink
Fix Primary Keys Representation + Some Additional Improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-w1lde committed Nov 2, 2024
1 parent 90d3573 commit f489513
Show file tree
Hide file tree
Showing 18 changed files with 124 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.furizon.backend.feature.pretix.event;

import com.google.common.hash.Hashing;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -8,6 +9,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.Map;

Expand All @@ -33,11 +35,16 @@ public class Event {
@Nullable
private Map<String, String> eventNames;

public int getId() {
return Hashing.sha512().hashString(slug, StandardCharsets.UTF_8).asInt();
}

public static class EventBuilder {
public EventBuilder slug(String fullSlug) {
this.slug = fullSlug;
return this;
}

public EventBuilder slug(String eventSlug, String organizerSlug) {
this.slug = PretixGenericUtils.buildOrgEventSlug(eventSlug, organizerSlug);
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.furizon.backend.feature.pretix.event.action.insert;

import com.google.common.hash.Hashing;
import lombok.RequiredArgsConstructor;
import net.furizon.backend.feature.pretix.event.Event;
import net.furizon.backend.infrastructure.jackson.JsonSerializer;
Expand All @@ -8,6 +9,7 @@
import org.jooq.util.postgres.PostgresDSL;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.Optional;

Expand All @@ -26,28 +28,24 @@ public void invoke(@NotNull Event event) {
PostgresDSL
.insertInto(
EVENTS,
EVENTS.ID,
EVENTS.EVENT_SLUG,
EVENTS.EVENT_DATE_TO,
EVENTS.EVENT_DATE_FROM,
EVENTS.EVENT_IS_CURRENT,
EVENTS.EVENT_PUBLIC_URL,
EVENTS.EVENT_NAMES
EVENTS.EVENT_NAMES_JSON
)
.values(
event.getId(),
event.getSlug(),
Optional
.ofNullable(event.getDateTo())
.map(OffsetDateTime::toString)
.orElse(null),
Optional
.ofNullable(event.getDateFrom())
.map(OffsetDateTime::toString)
.orElse(null),
event.getDateTo(),
event.getDateFrom(),
event.isCurrent(),
event.getPublicUrl(),
Optional
.ofNullable(event.getEventNames())
.map(jsonSerializer::serializeAsString)
.map(jsonSerializer::serializeAsJson)
.orElse(null)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,15 @@ public void invoke(@NotNull final Event event) {
command.execute(
PostgresDSL
.update(EVENTS)
.set(EVENTS.EVENT_DATE_FROM, Optional
.ofNullable(event.getDateFrom())
.map(OffsetDateTime::toString)
.orElse(null))
.set(EVENTS.EVENT_DATE_FROM, event.getDateFrom())
.set(EVENTS.EVENT_IS_CURRENT, event.isCurrent())
.set(EVENTS.EVENT_PUBLIC_URL, event.getPublicUrl())
.set(EVENTS.EVENT_NAMES, Optional
.set(EVENTS.EVENT_NAMES_JSON, Optional
.ofNullable(event.getEventNames())
.map(jsonSerializer::serializeAsString)
.orElse(null))
.set(EVENTS.EVENT_DATE_TO, Optional
.ofNullable(event.getDateTo())
.map(OffsetDateTime::toString)
.map(jsonSerializer::serializeAsJson)
.orElse(null))
.where(EVENTS.EVENT_SLUG.eq(event.getSlug()))
.set(EVENTS.EVENT_DATE_TO, event.getDateTo())
.where(EVENTS.ID.eq(event.getId()))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class JooqEventFinder implements EventFinder {
EVENTS.EVENT_DATE_FROM,
EVENTS.EVENT_IS_CURRENT,
EVENTS.EVENT_PUBLIC_URL,
EVENTS.EVENT_NAMES
EVENTS.EVENT_NAMES_JSON
)
.from(EVENTS)
.where(EVENTS.EVENT_SLUG.eq(slug))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.jooq.Record;
import org.springframework.stereotype.Component;

import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Optional;

Expand All @@ -28,23 +27,15 @@ public class JooqEventMapper {
public Event map(Record record) {
return Event.builder()
.slug(record.get(EVENTS.EVENT_SLUG))
.dateTo(
Optional.ofNullable(record.get(EVENTS.EVENT_DATE_TO))
.map(OffsetDateTime::parse)
.orElse(null)
)
.dateFrom(
Optional.ofNullable(record.get(EVENTS.EVENT_DATE_FROM))
.map(OffsetDateTime::parse)
.orElse(null)
)
.dateTo(record.get(EVENTS.EVENT_DATE_TO))
.dateFrom(record.get(EVENTS.EVENT_DATE_FROM))
.isCurrent(record.get(EVENTS.EVENT_IS_CURRENT))
.publicUrl(record.get(EVENTS.EVENT_PUBLIC_URL))
.eventNames(
Optional.ofNullable(record.get(EVENTS.EVENT_NAMES))
Optional.ofNullable(record.get(EVENTS.EVENT_NAMES_JSON))
.map(it -> {
try {
return objectMapper.readValue(it, typeRef);
return objectMapper.readValue(it.data(), typeRef);
} catch (JsonProcessingException e) {
log.error("Could not parse event names", e);
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.furizon.backend.feature.pretix.order;

import com.google.common.hash.Hashing;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -19,6 +20,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -73,15 +75,19 @@ public class Order {
private int answersMainPositionId = -1;

@Nullable
private User orderOwner; //TODO load from db! (lazy load?)
private User orderOwner;

@NotNull
private Event orderEvent; //TODO load from db! (lazy load?)
private Event orderEvent;

@NotNull
@Setter(AccessLevel.NONE)
private Map<String, Object> answers;

public int getId() {
return Hashing.sha512().hashString(code, StandardCharsets.UTF_8).asInt();
}

public boolean isDaily() {
return !dailyDays.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import lombok.RequiredArgsConstructor;
import net.furizon.backend.feature.pretix.order.Order;
import net.furizon.backend.feature.user.User;
import net.furizon.backend.infrastructure.jackson.JsonSerializer;
import net.furizon.backend.infrastructure.pretix.service.PretixInformation;
import net.furizon.jooq.infrastructure.command.SqlCommand;
import org.jetbrains.annotations.NotNull;
import org.jooq.util.postgres.PostgresDSL;
import org.springframework.stereotype.Component;

import java.util.Optional;

import static net.furizon.jooq.generated.Tables.ORDERS;

@Component
Expand All @@ -23,10 +26,14 @@ public void invoke(
@NotNull final Order order,
@NotNull final PretixInformation pretixInformation
) {
final var userId = Optional.ofNullable(order.getOrderOwner()).map(User::getId).orElse(null);
final var eventId = order.getOrderEvent().getId();

command.execute(
PostgresDSL
.insertInto(
ORDERS,
ORDERS.ID,
ORDERS.ORDER_CODE,
ORDERS.ORDER_STATUS,
ORDERS.ORDER_SPONSORSHIP_TYPE,
Expand All @@ -37,11 +44,12 @@ public void invoke(
ORDERS.ORDER_SECRET,
ORDERS.HAS_MEMBERSHIP,
ORDERS.ORDER_ANSWERS_MAIN_POSITION_ID,
//ORDERS.USER_ID, TODO
//ORDERS.EVENT_ID,
ORDERS.USER_ID,
ORDERS.EVENT_ID,
ORDERS.ORDER_ANSWERS_JSON
)
.values(
order.getId(),
order.getCode(),
(short) order.getOrderStatus().ordinal(),
(short) order.getSponsorship().ordinal(),
Expand All @@ -52,8 +60,8 @@ public void invoke(
order.getPretixOrderSecret(),
order.hasMembership(),
order.getAnswersMainPositionId(),
//order.getOrderOwner(),
//order.getOrderEvent(),
userId,
eventId,
jsonSerializer.serializeAsJson(order.getAllAnswers(pretixInformation))
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import lombok.RequiredArgsConstructor;
import net.furizon.backend.feature.pretix.order.Order;
import net.furizon.backend.feature.user.User;
import net.furizon.backend.infrastructure.jackson.JsonSerializer;
import net.furizon.backend.infrastructure.pretix.service.PretixInformation;
import net.furizon.jooq.infrastructure.command.SqlCommand;
import org.jetbrains.annotations.NotNull;
import org.jooq.util.postgres.PostgresDSL;
import org.springframework.stereotype.Component;

import java.util.Optional;

import static net.furizon.jooq.generated.Tables.ORDERS;

@Component
Expand All @@ -23,6 +26,8 @@ public void invoke(
@NotNull final Order order,
@NotNull final PretixInformation pretixInformation
) {
final var userId = Optional.ofNullable(order.getOrderOwner()).map(User::getId).orElse(null);

command.execute(
PostgresDSL
.update(ORDERS)
Expand All @@ -35,10 +40,10 @@ public void invoke(
.set(ORDERS.ORDER_SECRET, order.getPretixOrderSecret())
.set(ORDERS.HAS_MEMBERSHIP, order.hasMembership())
.set(ORDERS.ORDER_ANSWERS_MAIN_POSITION_ID, order.getAnswersMainPositionId())
//.set(ORDERS.USER_ID, order.getOrderOwner())
//.set(ORDERS.EVENT_ID, order.getOrderEvent())
.set(ORDERS.USER_ID, userId)
.set(ORDERS.EVENT_ID, order.getOrderEvent().getId())
.set(ORDERS.ORDER_ANSWERS_JSON, serializer.serializeAsJson(order.getAllAnswers(pretixInformation)))
.where(ORDERS.ORDER_CODE.eq(order.getCode()))
.where(ORDERS.ID.eq(order.getId()))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import lombok.RequiredArgsConstructor;
import net.furizon.backend.feature.pretix.order.Order;
import net.furizon.backend.feature.user.User;
import net.furizon.backend.infrastructure.jackson.JsonSerializer;
import net.furizon.backend.infrastructure.pretix.service.PretixInformation;
import net.furizon.jooq.infrastructure.command.SqlCommand;
import org.jetbrains.annotations.NotNull;
import org.jooq.util.postgres.PostgresDSL;
import org.springframework.stereotype.Component;

import java.util.Optional;

import static net.furizon.jooq.generated.Tables.ORDERS;

@Component
Expand All @@ -33,14 +36,15 @@ public void invoke(
String orderSecret = order.getPretixOrderSecret();
boolean membership = order.hasMembership();
int answersMainPositionId = order.getAnswersMainPositionId();
//User user = order.getOrderOwner();
//Event event = order.getOrderEvent();
final var userId = Optional.ofNullable(order.getOrderOwner()).map(User::getId).orElse(null);
final var eventId = order.getOrderEvent().getId();
final var answers = jsonSerializer.serializeAsJson(order.getAllAnswers(pretixInformation));

command.execute(
PostgresDSL
.insertInto(
ORDERS,
ORDERS.ID,
ORDERS.ORDER_CODE,
ORDERS.ORDER_STATUS,
ORDERS.ORDER_SPONSORSHIP_TYPE,
Expand All @@ -51,11 +55,12 @@ public void invoke(
ORDERS.ORDER_SECRET,
ORDERS.HAS_MEMBERSHIP,
ORDERS.ORDER_ANSWERS_MAIN_POSITION_ID,
//ORDERS.USER_ID, TODO
//ORDERS.EVENT_ID,
ORDERS.USER_ID,
ORDERS.EVENT_ID,
ORDERS.ORDER_ANSWERS_JSON
)
.values(
order.getId(),
code,
orderStatus,
sponsorship,
Expand All @@ -66,11 +71,11 @@ public void invoke(
orderSecret,
membership,
answersMainPositionId,
//user,
//event,
userId,
eventId,
answers
)
.onConflict(ORDERS.ORDER_CODE)
.onConflict(ORDERS.ID)
.doUpdate()
.set(ORDERS.ORDER_STATUS, orderStatus)
.set(ORDERS.ORDER_SPONSORSHIP_TYPE, sponsorship)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public class JooqOrderFinder implements OrderFinder {
EVENTS.EVENT_DATE_FROM,
EVENTS.EVENT_IS_CURRENT,
EVENTS.EVENT_PUBLIC_URL,
EVENTS.EVENT_NAMES
EVENTS.EVENT_NAMES_JSON
)
.from(ORDERS)
.leftOuterJoin(USERS)
.on(USERS.USER_ID.eq(ORDERS.USER_ID))
.leftOuterJoin(EVENTS)
.on(EVENTS.EVENT_SLUG.eq(ORDERS.EVENT_ID))
.on(EVENTS.ID.eq(ORDERS.EVENT_ID))
.where(ORDERS.ORDER_CODE.eq(code))
).mapOrNull(orderMapper::map);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public class ReloadOrdersUseCase implements UseCase<ReloadOrdersUseCase.Input, B
@NotNull
private final PretixOrderFinder pretixOrderFinder;

// TODO -> Do we need InsertNewOrderAction if we have UpsertOrderAction?
@NotNull
private final UpsertOrderAction insertOrUpdateOrderAction;
private final UpsertOrderAction upsertOrderAction;

@NotNull
private final DeleteOrderAction deleteOrderAction;
Expand All @@ -49,7 +50,7 @@ public Boolean executor(@NotNull Input input) {
Order order = orderOpt.get();
OrderStatus os = order.getOrderStatus();
if (os == OrderStatus.PENDING || os == OrderStatus.PAID) {
insertOrUpdateOrderAction.invoke(order, input.pretixInformation);
upsertOrderAction.invoke(order, input.pretixInformation);
shouldDelete = false;
}
}
Expand Down
2 changes: 2 additions & 0 deletions db/migrations/000001_model.down.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
DROP INDEX IF EXISTS event_slug_idx;

DROP TABLE IF EXISTS events;
DROP TABLE IF EXISTS "groups";
DROP TABLE IF EXISTS users;
Expand Down
Loading

0 comments on commit f489513

Please sign in to comment.