Skip to content

Commit

Permalink
Implement some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-w1lde committed Nov 1, 2024
1 parent a9eb769 commit a57186a
Show file tree
Hide file tree
Showing 29 changed files with 374 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void invoke(@NotNull Event event) {
event.getPublicUrl(),
Optional
.ofNullable(event.getEventNames())
.map(jsonSerializer::serialize)
.map(jsonSerializer::serializeAsString)
.orElse(null)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void invoke(@NotNull Event event) {
.set(EVENTS.EVENT_PUBLIC_URL, event.getPublicUrl())
.set(EVENTS.EVENT_NAMES, Optional
.ofNullable(event.getEventNames())
.map(jsonSerializer::serialize)
.map(jsonSerializer::serializeAsString)
.orElse(null))
.set(EVENTS.EVENT_DATE_TO, Optional
.ofNullable(event.getDateTo())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -139,6 +140,39 @@ public long getDailyDaysBitmask() {
return ret;
}

public List<PretixAnswer> getAllAnswers(@NotNull PretixInformation pretixInformation) {
final var list = new ArrayList<PretixAnswer>();
final var answers = getAnswers();
for (String key : answers.keySet()) {
final var questionId = pretixInformation.getQuestionIdFromIdentifier(key);
if (questionId.isEmpty()) {
continue;
}

int id = questionId.get();
var type = pretixInformation.getQuestionTypeFromId(id);
if (type.isEmpty()) {
continue;
}
Object o = answers.get(key);
String out = switch (type.get()) {
case NUMBER -> Float.toString((float) o);
case STRING_ONE_LINE, FILE, COUNTRY_CODE, PHONE_NUMBER, STRING_MULTI_LINE, LIST_SINGLE_CHOICE ->
(String) o;
case BOOLEAN -> ((boolean) o) ? "True" : "False"; //fuck python
case LIST_MULTIPLE_CHOICE -> String.join(", ", (String[]) o);
case DATE, TIME -> o.toString();
case DATE_TIME -> ((ZonedDateTime) o).format(PretixGenericUtils.PRETIX_DATETIME_FORMAT);
};

if (out != null && !(out = out.strip()).isEmpty()) {
list.add(new PretixAnswer(id, out));
}
}

return list;
}

public static class OrderBuilder {
public OrderBuilder dailyDays(Set<Integer> days) {
dailyDays = days;
Expand All @@ -160,27 +194,31 @@ public OrderBuilder answers(@NotNull List<PretixAnswer> answers, @NotNull Pretix
for (PretixAnswer answer : answers) {
int questionId = answer.getQuestionId();
var identifier = pi.getQuestionIdentifierFromId(questionId);
if (identifier.isPresent()) {
String answerIdentifier = identifier.get();
String value = answer.getAnswer();
if (value != null) {
var type = pi.getQuestionTypeFromId(questionId);
if (type.isPresent()) {
Object o = switch (type.get()) {
case NUMBER -> Float.parseFloat(value);
case STRING_ONE_LINE, STRING_MULTI_LINE, COUNTRY_CODE, PHONE_NUMBER,
LIST_SINGLE_CHOICE -> value;
case BOOLEAN -> value.equalsIgnoreCase("true")
|| value.equalsIgnoreCase("yes");
case LIST_MULTIPLE_CHOICE -> value.split(", ");
case FILE -> Const.QUESTIONS_FILE_KEEP;
case DATE -> LocalDate.parse(value);
case TIME -> LocalTime.parse(value);
case DATE_TIME -> ZonedDateTime.parse(value, PretixGenericUtils.PRETIX_DATETIME_FORMAT);
};
this.answers.put(answerIdentifier, o);
}
}
if (identifier.isEmpty()) {
throw new IllegalArgumentException("Answer identifier is empty");
}

String answerIdentifier = identifier.get();
String value = answer.getAnswer();
if (value == null) {
throw new IllegalArgumentException("Answer " + answerIdentifier + " has null value");
}

var type = pi.getQuestionTypeFromId(questionId);
if (type.isPresent()) {
Object o = switch (type.get()) {
case NUMBER -> Float.parseFloat(value);
case STRING_ONE_LINE, STRING_MULTI_LINE, COUNTRY_CODE, PHONE_NUMBER,
LIST_SINGLE_CHOICE -> value;
case BOOLEAN -> value.equalsIgnoreCase("true")
|| value.equalsIgnoreCase("yes");
case LIST_MULTIPLE_CHOICE -> value.split(", ");
case FILE -> Const.QUESTIONS_FILE_KEEP;
case DATE -> LocalDate.parse(value);
case TIME -> LocalTime.parse(value);
case DATE_TIME -> ZonedDateTime.parse(value, PretixGenericUtils.PRETIX_DATETIME_FORMAT);
};
this.answers.put(answerIdentifier, o);
}
}
return this;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package net.furizon.backend.feature.pretix.order.action.insertNewOrder;

import net.furizon.backend.feature.pretix.order.Order;
import net.furizon.backend.infrastructure.pretix.service.PretixInformation;
import org.jetbrains.annotations.NotNull;

public interface InsertNewOrderAction {
void invoke(@NotNull Order order);
void invoke(
@NotNull final Order order,
@NotNull final PretixInformation pretixInformation
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import lombok.RequiredArgsConstructor;
import net.furizon.backend.feature.pretix.order.Order;
import net.furizon.backend.feature.pretix.order.util.OrderTransformationUtil;
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;
Expand All @@ -15,10 +16,13 @@
public class JooqInsertNewOrderAction implements InsertNewOrderAction {
private final SqlCommand command;

private final OrderTransformationUtil orderTransformationUtil;
private final JsonSerializer jsonSerializer;

@Override
public void invoke(@NotNull Order order) {
public void invoke(
@NotNull final Order order,
@NotNull final PretixInformation pretixInformation
) {
command.execute(
PostgresDSL
.insertInto(
Expand All @@ -35,7 +39,7 @@ public void invoke(@NotNull Order order) {
ORDERS.ORDER_ANSWERS_MAIN_POSITION_ID,
//ORDERS.USER_ID, TODO
//ORDERS.EVENT_ID,
ORDERS.ORDER_ANSWERS
ORDERS.ORDER_ANSWERS_JSON
)
.values(
order.getCode(),
Expand All @@ -50,7 +54,7 @@ public void invoke(@NotNull Order order) {
order.getAnswersMainPositionId(),
//order.getOrderOwner(),
//order.getOrderEvent(),
orderTransformationUtil.getAnswersAsJson(order)
jsonSerializer.serializeAsJson(order.getAllAnswers(pretixInformation))
)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.furizon.backend.feature.pretix.order.action.pushAnswer;

import net.furizon.backend.feature.pretix.order.Order;
import net.furizon.backend.infrastructure.pretix.service.PretixInformation;
import org.jetbrains.annotations.NotNull;

public interface PushPretixAnswerAction {
boolean invoke(
@NotNull final Order order,
@NotNull final PretixInformation pretixInformation
);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package net.furizon.backend.feature.pretix.order.action.addAnswer;
package net.furizon.backend.feature.pretix.order.action.pushAnswer;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.furizon.backend.feature.pretix.event.Event;
import net.furizon.backend.feature.pretix.order.Order;
import net.furizon.backend.feature.pretix.order.util.OrderTransformationUtil;
import net.furizon.backend.feature.pretix.order.dto.PushPretixAnswerRequest;
import net.furizon.backend.infrastructure.http.client.HttpClient;
import net.furizon.backend.infrastructure.http.client.HttpRequest;
import net.furizon.backend.infrastructure.pretix.PretixConfig;
import net.furizon.backend.infrastructure.pretix.service.PretixInformation;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpMethod;
Expand All @@ -19,25 +19,25 @@
@Component
@RequiredArgsConstructor
@Slf4j
public class RestAddPretixAnswerAction implements AddPretixAnswerAction {
public class RestPushPretixAnswerAction implements PushPretixAnswerAction {
@Qualifier("pretixHttpClient")
private final HttpClient pretixHttpClient;

private final OrderTransformationUtil orderTransformation;

@Override
public boolean invoke(
@NotNull final Order order,
@NotNull final Event.OrganizerAndEventPair pair
@NotNull final PretixInformation pretixInformation
) {
final var request = HttpRequest.create()
final var pair = order.getOrderEvent().getOrganizerAndEventPair();
final var request = HttpRequest.<Void>create()
.method(HttpMethod.PATCH)
.path("/organizers/{organizer}/events/{event}/orderpositions/{position}/")
.uriVariable("organizer", pair.getOrganizer())
.uriVariable("event", pair.getEvent())
.uriVariable("position", String.valueOf(order.getAnswersMainPositionId()))
.contentType(MediaType.APPLICATION_JSON)
.body(orderTransformation.getAnswersAsJson(order))
.body(new PushPretixAnswerRequest(order.getAllAnswers(pretixInformation)))
.responseType(Void.class)
.build();
try {
return pretixHttpClient.send(PretixConfig.class, request).getStatusCode() == HttpStatus.OK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import lombok.RequiredArgsConstructor;
import net.furizon.backend.feature.pretix.order.Order;
import net.furizon.backend.feature.pretix.order.util.OrderTransformationUtil;
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;
Expand All @@ -15,10 +16,13 @@
public class JooqUpdateOrderAction implements UpdateOrderAction {
private final SqlCommand command;

private final OrderTransformationUtil orderTransformationUtil;
private final JsonSerializer serializer;

@Override
public void invoke(@NotNull Order order) {
public void invoke(
@NotNull final Order order,
@NotNull final PretixInformation pretixInformation
) {
command.execute(
PostgresDSL
.update(ORDERS)
Expand All @@ -33,7 +37,7 @@ public void invoke(@NotNull Order order) {
.set(ORDERS.ORDER_ANSWERS_MAIN_POSITION_ID, order.getAnswersMainPositionId())
//.set(ORDERS.USER_ID, order.getOrderOwner())
//.set(ORDERS.EVENT_ID, order.getOrderEvent())
.set(ORDERS.ORDER_ANSWERS, orderTransformationUtil.getAnswersAsJson(order))
.set(ORDERS.ORDER_ANSWERS_JSON, serializer.serializeAsJson(order.getAllAnswers(pretixInformation)))
.where(ORDERS.ORDER_CODE.eq(order.getCode()))
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package net.furizon.backend.feature.pretix.order.action.updateOrder;

import net.furizon.backend.feature.pretix.order.Order;
import net.furizon.backend.infrastructure.pretix.service.PretixInformation;
import org.jetbrains.annotations.NotNull;

public interface UpdateOrderAction {
void invoke(@NotNull Order order);
void invoke(
@NotNull final Order order,
@NotNull final PretixInformation pretixInformation
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import lombok.RequiredArgsConstructor;
import net.furizon.backend.feature.pretix.order.Order;
import net.furizon.backend.feature.pretix.order.util.OrderTransformationUtil;
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;
Expand All @@ -15,10 +16,10 @@
public class JooqUpsertOrderAction implements UpsertOrderAction {
private final SqlCommand command;

private final OrderTransformationUtil orderTransformationUtil;
private final JsonSerializer jsonSerializer;

@Override
public void invoke(@NotNull Order order) {
public void invoke(@NotNull Order order, @NotNull PretixInformation pretixInformation) {
String code = order.getCode();
short orderStatus = (short) order.getOrderStatus().ordinal();
short sponsorship = (short) order.getSponsorship().ordinal();
Expand All @@ -31,7 +32,7 @@ public void invoke(@NotNull Order order) {
int answersMainPositionId = order.getAnswersMainPositionId();
//User user = order.getOrderOwner();
//Event event = order.getOrderEvent();
String answersJson = orderTransformationUtil.getAnswersAsJson(order);
final var answers = jsonSerializer.serializeAsJson(order.getAllAnswers(pretixInformation));


command.execute(
Expand All @@ -50,7 +51,7 @@ public void invoke(@NotNull Order order) {
ORDERS.ORDER_ANSWERS_MAIN_POSITION_ID,
//ORDERS.USER_ID, TODO
//ORDERS.EVENT_ID,
ORDERS.ORDER_ANSWERS
ORDERS.ORDER_ANSWERS_JSON
)
.values(
code,
Expand All @@ -65,7 +66,7 @@ public void invoke(@NotNull Order order) {
answersMainPositionId,
//user,
//event,
answersJson
answers
)
.onConflict(ORDERS.ORDER_CODE)
.doUpdate()
Expand All @@ -80,7 +81,7 @@ public void invoke(@NotNull Order order) {
.set(ORDERS.ORDER_ANSWERS_MAIN_POSITION_ID, answersMainPositionId)
//.set(ORDERS.USER_ID, user)
//.set(ORDERS.EVENT_ID, event)
.set(ORDERS.ORDER_ANSWERS, answersJson)
.set(ORDERS.ORDER_ANSWERS_JSON, answers)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package net.furizon.backend.feature.pretix.order.action.upsertOrder;

import net.furizon.backend.feature.pretix.order.Order;
import net.furizon.backend.infrastructure.pretix.service.PretixInformation;
import org.jetbrains.annotations.NotNull;

public interface UpsertOrderAction {
void invoke(@NotNull Order order);
void invoke(@NotNull Order order, @NotNull PretixInformation pretixInformation);
}
Loading

0 comments on commit a57186a

Please sign in to comment.