added missing changes to perious commit
This commit is contained in:
parent
f657ff2c80
commit
3b05befa92
2 changed files with 41 additions and 22 deletions
|
|
@ -7,11 +7,15 @@ import de.avatic.lcc.util.exception.base.ForbiddenException;
|
|||
import de.avatic.lcc.util.exception.internalerror.DatabaseException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
||||
import org.springframework.jdbc.support.KeyHolder;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.*;
|
||||
|
||||
@Repository
|
||||
|
|
@ -24,7 +28,7 @@ public class UserNodeRepository {
|
|||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
|
||||
@Transactional
|
||||
public Collection<Node> searchNode(String filter, int limit, int userId, boolean excludeDeprecated) {
|
||||
|
||||
StringBuilder queryBuilder = new StringBuilder().append("SELECT * FROM sys_user_node WHERE user_id = ?");
|
||||
|
|
@ -49,28 +53,36 @@ public class UserNodeRepository {
|
|||
return jdbcTemplate.query(queryBuilder.toString(), new NodeMapper(), params.toArray());
|
||||
}
|
||||
|
||||
public void add(Integer userId, Node node) {
|
||||
@Transactional
|
||||
public Integer add(Integer userId, Node node) {
|
||||
String sql = """
|
||||
INSERT INTO sys_user_node (
|
||||
name, address, geo_lat, geo_lng,
|
||||
is_deprecated, country_id, user_id
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
""";
|
||||
INSERT INTO sys_user_node (
|
||||
name, address, geo_lat, geo_lng,
|
||||
is_deprecated, country_id, user_id
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
""";
|
||||
|
||||
var affectedRows = jdbcTemplate.update(sql,
|
||||
node.getName(),
|
||||
node.getAddress(),
|
||||
node.getGeoLat(),
|
||||
node.getGeoLng(),
|
||||
node.getDeprecated(),
|
||||
node.getCountryId(),
|
||||
userId
|
||||
);
|
||||
KeyHolder keyHolder = new GeneratedKeyHolder();
|
||||
|
||||
if(affectedRows != 1)
|
||||
int affectedRows = jdbcTemplate.update(connection -> {
|
||||
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
ps.setString(1, node.getName());
|
||||
ps.setString(2, node.getAddress());
|
||||
ps.setBigDecimal(3, node.getGeoLat());
|
||||
ps.setBigDecimal(4, node.getGeoLng());
|
||||
ps.setBoolean(5, node.getDeprecated());
|
||||
ps.setInt(6, node.getCountryId());
|
||||
ps.setInt(7, userId);
|
||||
return ps;
|
||||
}, keyHolder);
|
||||
|
||||
if (affectedRows != 1)
|
||||
throw new DatabaseException("Could not add node to user");
|
||||
|
||||
return Objects.requireNonNull(keyHolder.getKey()).intValue();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Optional<Node> getById(Integer id) {
|
||||
String query = "SELECT * FROM sys_user_node WHERE id = ?";
|
||||
|
||||
|
|
@ -82,6 +94,7 @@ public class UserNodeRepository {
|
|||
return Optional.of(nodes.getFirst());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Node> findNodeListsForReportingByPeriodId(Integer materialId, ValidityTuple tuple) {
|
||||
String userSuppliersSql = """
|
||||
SELECT DISTINCT un.*
|
||||
|
|
@ -99,6 +112,7 @@ public class UserNodeRepository {
|
|||
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Collection<Node> getByIds(List<Integer> nodeIds) {
|
||||
String placeholders = String.join(",", Collections.nCopies(nodeIds.size(), "?"));
|
||||
String query = """
|
||||
|
|
@ -109,6 +123,7 @@ public class UserNodeRepository {
|
|||
return nodeIds.isEmpty() ? Collections.emptyList() : jdbcTemplate.query(query, new NodeMapper(), nodeIds.toArray());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Optional<Integer> getOwnerById(Integer userSupplierId) {
|
||||
|
||||
String query = """
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
package de.avatic.lcc.service.access;
|
||||
|
||||
import de.avatic.lcc.dto.configuration.nodes.userNodes.AddUserNodeDTO;
|
||||
import de.avatic.lcc.dto.generic.NodeDTO;
|
||||
import de.avatic.lcc.model.country.IsoCode;
|
||||
import de.avatic.lcc.model.nodes.Node;
|
||||
import de.avatic.lcc.repositories.country.CountryRepository;
|
||||
import de.avatic.lcc.repositories.users.UserNodeRepository;
|
||||
import de.avatic.lcc.service.transformer.generic.NodeTransformer;
|
||||
import de.avatic.lcc.service.users.AuthorizationService;
|
||||
import de.avatic.lcc.util.exception.badrequest.InvalidArgumentException;
|
||||
import de.avatic.lcc.util.exception.badrequest.NotFoundException;
|
||||
|
|
@ -17,11 +19,13 @@ public class UserNodeService {
|
|||
private final CountryRepository countryRepository;
|
||||
private final UserNodeRepository userNodeRepository;
|
||||
private final AuthorizationService authorizationService;
|
||||
private final NodeTransformer nodeTransformer;
|
||||
|
||||
public UserNodeService(CountryRepository countryRepository, UserNodeRepository userNodeRepository, AuthorizationService authorizationService) {
|
||||
public UserNodeService(CountryRepository countryRepository, UserNodeRepository userNodeRepository, AuthorizationService authorizationService, NodeTransformer nodeTransformer) {
|
||||
this.countryRepository = countryRepository;
|
||||
this.userNodeRepository = userNodeRepository;
|
||||
this.authorizationService = authorizationService;
|
||||
this.nodeTransformer = nodeTransformer;
|
||||
}
|
||||
|
||||
public Node getUserNode(Integer id) {
|
||||
|
|
@ -35,9 +39,7 @@ public class UserNodeService {
|
|||
* address, geographic location, and country information.
|
||||
* @throws IllegalArgumentException if any required information in the DTO is missing or invalid.
|
||||
*/
|
||||
public void addUserNode(AddUserNodeDTO dto) {
|
||||
|
||||
// TODO: get real user id.
|
||||
public NodeDTO addUserNode(AddUserNodeDTO dto) {
|
||||
var userId = authorizationService.getUserId();
|
||||
|
||||
Node node = new Node();
|
||||
|
|
@ -60,6 +62,8 @@ public class UserNodeService {
|
|||
|
||||
node.setCountryId(countryRepository.getByIsoCode(IsoCode.valueOf(dto.getCountry().getIsoCode())).orElseThrow(() -> new NotFoundException(NotFoundException.NotFoundType.COUNTRY, "iso code", dto.getCountry().getIsoCode())).getId());
|
||||
|
||||
userNodeRepository.add(userId, node);
|
||||
var id = userNodeRepository.add(userId, node);
|
||||
|
||||
return nodeTransformer.toNodeDTO(userNodeRepository.getById(id).orElseThrow());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue