Skip to content

Commit 94c71c5

Browse files
committed
feat: add api for chargebox/reservation/user
1 parent e4da970 commit 94c71c5

38 files changed

Lines changed: 1088 additions & 338 deletions

src/main/java/de/rwth/idsg/steve/repository/ReservationRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
import org.jooq.Select;
2626

2727
import java.util.List;
28+
import java.util.Optional;
2829

2930
/**
3031
* @author Sevket Goekay <sevketgokay@gmail.com>
3132
* @since 19.08.2014
3233
*/
3334
public interface ReservationRepository {
35+
Optional<Reservation> getReservation(int id);
3436
List<Reservation> getReservations(ReservationQueryForm form);
3537

3638
List<Integer> getActiveReservationIds(String chargeBoxId);

src/main/java/de/rwth/idsg/steve/repository/UserRepository.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@
2323
import de.rwth.idsg.steve.web.dto.UserQueryForm;
2424

2525
import java.util.List;
26+
import java.util.Optional;
2627

2728
/**
2829
* @author Sevket Goekay <sevketgokay@gmail.com>
2930
* @since 25.11.2015
3031
*/
3132
public interface UserRepository {
3233
List<User.Overview> getOverview(UserQueryForm form);
33-
User.Details getDetails(int userPk);
34+
Optional<User.Details> getDetails(int userPk);
3435

35-
void add(UserForm form);
36+
Integer add(UserForm form);
3637
void update(UserForm form);
3738
void delete(int userPk);
3839
}

src/main/java/de/rwth/idsg/steve/repository/impl/ReservationRepositoryImpl.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import de.rwth.idsg.steve.repository.dto.Reservation;
2626
import de.rwth.idsg.steve.utils.DateTimeUtils;
2727
import de.rwth.idsg.steve.web.dto.ReservationQueryForm;
28+
import lombok.RequiredArgsConstructor;
2829
import lombok.extern.slf4j.Slf4j;
2930
import org.joda.time.DateTime;
3031
import org.jooq.DSLContext;
@@ -36,10 +37,10 @@
3637
import org.jooq.SelectQuery;
3738
import org.jooq.exception.DataAccessException;
3839
import org.jooq.impl.DSL;
39-
import org.springframework.beans.factory.annotation.Autowired;
4040
import org.springframework.stereotype.Repository;
4141

4242
import java.util.List;
43+
import java.util.Optional;
4344

4445
import static jooq.steve.db.tables.ChargeBox.CHARGE_BOX;
4546
import static jooq.steve.db.tables.Connector.CONNECTOR;
@@ -52,13 +53,34 @@
5253
*/
5354
@Slf4j
5455
@Repository
56+
@RequiredArgsConstructor
5557
public class ReservationRepositoryImpl implements ReservationRepository {
5658

5759
private final DSLContext ctx;
5860

59-
@Autowired
60-
public ReservationRepositoryImpl(DSLContext ctx) {
61-
this.ctx = ctx;
61+
@Override
62+
public Optional<Reservation> getReservation(int id) {
63+
SelectQuery selectQuery = ctx.selectQuery();
64+
selectQuery.addFrom(RESERVATION);
65+
selectQuery.addJoin(OCPP_TAG, OCPP_TAG.ID_TAG.eq(RESERVATION.ID_TAG));
66+
selectQuery.addJoin(CONNECTOR, CONNECTOR.CONNECTOR_PK.eq(RESERVATION.CONNECTOR_PK));
67+
selectQuery.addJoin(CHARGE_BOX, CONNECTOR.CHARGE_BOX_ID.eq(CHARGE_BOX.CHARGE_BOX_ID));
68+
69+
selectQuery.addSelect(
70+
RESERVATION.RESERVATION_PK,
71+
RESERVATION.TRANSACTION_PK,
72+
OCPP_TAG.OCPP_TAG_PK,
73+
CHARGE_BOX.CHARGE_BOX_PK,
74+
OCPP_TAG.ID_TAG,
75+
CHARGE_BOX.CHARGE_BOX_ID,
76+
RESERVATION.START_DATETIME,
77+
RESERVATION.EXPIRY_DATETIME,
78+
RESERVATION.STATUS,
79+
CONNECTOR.CONNECTOR_ID
80+
);
81+
selectQuery.addConditions(RESERVATION.RESERVATION_PK.eq(id));
82+
Reservation result = (Reservation) selectQuery.fetchOne(new ReservationMapper());
83+
return Optional.ofNullable(result);
6284
}
6385

6486
@Override

src/main/java/de/rwth/idsg/steve/repository/impl/UserRepositoryImpl.java

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,10 @@
3737
import org.jooq.SelectConditionStep;
3838
import org.jooq.exception.DataAccessException;
3939
import org.jooq.impl.DSL;
40-
import org.springframework.beans.factory.annotation.Autowired;
4140
import org.springframework.stereotype.Repository;
4241
import org.springframework.util.CollectionUtils;
4342

44-
import java.util.ArrayList;
45-
import java.util.HashMap;
46-
import java.util.List;
47-
import java.util.Map;
43+
import java.util.*;
4844

4945
import static de.rwth.idsg.steve.utils.CustomDSL.includes;
5046
import static jooq.steve.db.Tables.USER_OCPP_TAG;
@@ -79,31 +75,31 @@ public List<User.Overview> getOverview(UserQueryForm form) {
7975
}
8076

8177
@Override
82-
public User.Details getDetails(int userPk) {
83-
UserRecord ur = ctx.selectFrom(USER)
78+
public Optional<User.Details> getDetails(int userPk) {
79+
var ur = ctx.selectFrom(USER)
8480
.where(USER.USER_PK.equal(userPk))
8581
.fetchOne();
8682

8783
if (ur == null) {
88-
throw new SteveException("There is no user with id '%s'", userPk);
84+
return Optional.empty();
8985
}
9086

91-
return User.Details.builder()
87+
return Optional.of(User.Details.builder()
9288
.userRecord(ur)
9389
.address(addressRepository.get(ctx, ur.getAddressPk()))
9490
.ocppTagEntries(getOcppTagsInternal(userPk, null).getOrDefault(userPk, List.of()))
95-
.build();
91+
.build());
9692
}
9793

9894
@Override
99-
public void add(UserForm form) {
100-
ctx.transaction(configuration -> {
101-
DSLContext ctx = DSL.using(configuration);
95+
public Integer add(UserForm form) {
96+
return ctx.transactionResult(configuration -> {
97+
var ctx = DSL.using(configuration);
10298
try {
103-
Integer addressId = addressRepository.updateOrInsert(ctx, form.getAddress());
104-
Integer userPk = addInternal(ctx, form, addressId);
99+
var addressId = addressRepository.updateOrInsert(ctx, form.getAddress());
100+
var userPk = addInternal(ctx, form, addressId);
105101
refreshOcppTagsInternal(ctx, form, userPk);
106-
102+
return userPk;
107103
} catch (DataAccessException e) {
108104
throw new SteveException("Failed to add the user", e);
109105
}
@@ -113,9 +109,9 @@ public void add(UserForm form) {
113109
@Override
114110
public void update(UserForm form) {
115111
ctx.transaction(configuration -> {
116-
DSLContext ctx = DSL.using(configuration);
112+
var ctx = DSL.using(configuration);
117113
try {
118-
Integer addressId = addressRepository.updateOrInsert(ctx, form.getAddress());
114+
var addressId = addressRepository.updateOrInsert(ctx, form.getAddress());
119115
updateInternal(ctx, form, addressId);
120116
refreshOcppTagsInternal(ctx, form, form.getUserPk());
121117

@@ -128,11 +124,10 @@ public void update(UserForm form) {
128124
@Override
129125
public void delete(int userPk) {
130126
ctx.transaction(configuration -> {
131-
DSLContext ctx = DSL.using(configuration);
127+
var ctx = DSL.using(configuration);
132128
try {
133129
addressRepository.delete(ctx, selectAddressId(userPk));
134130
deleteInternal(ctx, userPk);
135-
136131
} catch (DataAccessException e) {
137132
throw new SteveException("Failed to delete the user", e);
138133
}
@@ -186,7 +181,7 @@ private Result<Record5<Integer, String, String, String, String>> getOverviewInte
186181
}
187182

188183
private Map<Integer, List<User.OcppTagEntry>> getOcppTagsInternal(Integer userPk, String ocppIdTag) {
189-
List<Condition> conditions = new ArrayList<>();
184+
var conditions = new ArrayList<Condition>();
190185

191186
if (userPk != null) {
192187
conditions.add(USER_OCPP_TAG.USER_PK.eq(userPk));
@@ -205,7 +200,7 @@ private Map<Integer, List<User.OcppTagEntry>> getOcppTagsInternal(Integer userPk
205200
.where(conditions)
206201
.fetch();
207202

208-
Map<Integer, List<User.OcppTagEntry>> map = new HashMap<>();
203+
var map = new HashMap<Integer, List<User.OcppTagEntry>>();
209204
for (var entry : results) {
210205
map.computeIfAbsent(entry.value1(), k -> new ArrayList<>())
211206
.add(new User.OcppTagEntry(entry.value2(), entry.value3()));
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* SteVe - SteckdosenVerwaltung - https://github.com/steve-community/steve
3+
* Copyright (C) 2013-2025 SteVe Community Team
4+
* All Rights Reserved.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package de.rwth.idsg.steve.service;
20+
21+
import de.rwth.idsg.steve.repository.ChargePointRepository;
22+
import de.rwth.idsg.steve.repository.dto.ChargePoint;
23+
import de.rwth.idsg.steve.web.dto.ChargePointForm;
24+
import de.rwth.idsg.steve.web.dto.ChargePointQueryForm;
25+
import lombok.RequiredArgsConstructor;
26+
import org.springframework.stereotype.Service;
27+
28+
import java.util.List;
29+
30+
@Service
31+
@RequiredArgsConstructor
32+
public class ChargeboxService {
33+
34+
private final ChargePointRepository chargePointRepository;
35+
36+
public List<ChargePoint.Overview> getOverview(ChargePointQueryForm form) {
37+
return chargePointRepository.getOverview(form);
38+
}
39+
40+
public ChargePoint.Details getDetails(int chargeBoxPk) {
41+
return chargePointRepository.getDetails(chargeBoxPk);
42+
}
43+
44+
public int addChargePoint(ChargePointForm form) {
45+
return chargePointRepository.addChargePoint(form);
46+
}
47+
48+
public void updateChargePoint(ChargePointForm form) {
49+
chargePointRepository.updateChargePoint(form);
50+
}
51+
52+
public void deleteChargePoint(int chargeBoxPk) {
53+
chargePointRepository.deleteChargePoint(chargeBoxPk);
54+
}
55+
56+
public List<String> getChargeBoxIds() {
57+
return chargePointRepository.getChargeBoxIds();
58+
}
59+
60+
public void addChargePointList(List<String> chargeBoxIdList) {
61+
chargePointRepository.addChargePointList(chargeBoxIdList);
62+
}
63+
64+
public List<Integer> getNonZeroConnectorIds(String chargeBoxId) {
65+
return chargePointRepository.getNonZeroConnectorIds(chargeBoxId);
66+
}
67+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* SteVe - SteckdosenVerwaltung - https://github.com/steve-community/steve
3+
* Copyright (C) 2013-2025 SteVe Community Team
4+
* All Rights Reserved.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package de.rwth.idsg.steve.service;
20+
21+
import de.rwth.idsg.steve.repository.ReservationRepository;
22+
import de.rwth.idsg.steve.repository.dto.InsertReservationParams;
23+
import de.rwth.idsg.steve.repository.dto.Reservation;
24+
import de.rwth.idsg.steve.web.dto.ReservationForm;
25+
import de.rwth.idsg.steve.web.dto.ReservationQueryForm;
26+
import lombok.RequiredArgsConstructor;
27+
import org.springframework.stereotype.Service;
28+
29+
import java.util.List;
30+
import java.util.Optional;
31+
32+
@Service
33+
@RequiredArgsConstructor
34+
public class ReservationService {
35+
36+
private final ReservationRepository reservationRepository;
37+
38+
public List<Reservation> getReservations(ReservationQueryForm form) {
39+
return reservationRepository.getReservations(form);
40+
}
41+
42+
public int addReservation(ReservationForm form) {
43+
var params = InsertReservationParams.builder()
44+
.idTag(form.getIdTag())
45+
.chargeBoxId(form.getChargeBoxId())
46+
.connectorId(form.getConnectorId())
47+
.startTimestamp(form.getStartTimestamp())
48+
.expiryTimestamp(form.getExpiryTimestamp())
49+
.build();
50+
return reservationRepository.insert(params);
51+
}
52+
53+
public void deleteReservation(int reservationId) {
54+
reservationRepository.delete(reservationId);
55+
}
56+
57+
public Optional<Reservation> getReservation(int id) {
58+
return reservationRepository.getReservation(id);
59+
}
60+
61+
public List<Integer> getActiveReservationIds(String chargeBoxId) {
62+
return reservationRepository.getActiveReservationIds(chargeBoxId);
63+
}
64+
}

0 commit comments

Comments
 (0)