Code entity renamed to Gate

This commit is contained in:
Universall 2025-02-19 12:14:44 +03:00
parent 52f900ec4d
commit f6f3289fed
24 changed files with 163 additions and 160 deletions

View File

@ -1,7 +1,7 @@
package com.displaynone.acss.components.acs;
import com.displaynone.acss.components.acs.code.CodeModel;
import com.displaynone.acss.components.acs.code.service.CodeService;
import com.displaynone.acss.components.acs.gate.GateModel;
import com.displaynone.acss.components.acs.gate.service.GateService;
import com.displaynone.acss.components.acs.visit.VisitModel;
import com.displaynone.acss.components.acs.visit.service.VisitService;
import lombok.RequiredArgsConstructor;
@ -15,43 +15,43 @@ import java.util.Optional;
@Component
@RequiredArgsConstructor
public class ACSComponent {
private final CodeService codeService;
private final GateService gateService;
private final VisitService visitService;
public Optional<CodeModel> getCodeById(Long codeId) {
return codeService.getCodeById(codeId);
public Optional<GateModel> getGateById(Long gateId) {
return gateService.getGateById(gateId);
}
public CodeModel getCodeByIdStrict(Long codeId) {
return codeService.getCodeByIdStrict(codeId);
public GateModel getGateByIdStrict(Long gateId) {
return gateService.getGateByIdStrict(gateId);
}
public Optional<CodeModel> getCodeByValue(Long value) {
return codeService.getCodeByValue(value);
public Optional<GateModel> getGateByCode(Long code) {
return gateService.getGateByCode(code);
}
public CodeModel getCodeByValueStrict(Long value) {
return codeService.getCodeByValueStrict(value);
public GateModel getGateByCodeStrict(Long code) {
return gateService.getGateByCodeStrict(code);
}
public boolean isCodeIdValid(Long codeId) {
return codeService.isValid(codeId);
public boolean isGateExists(Long gateId) {
return gateService.isGateExists(gateId);
}
public boolean isCodeValid(Long codeId) {
return codeService.isCodeIdValid(codeId);
public boolean isGateCodeValid(Long code) {
return gateService.isGateCodeValid(code);
}
public List<VisitModel> getVisitsByCodeId(Long codeId) {
return visitService.getByCodeId(codeId);
public List<VisitModel> getVisitsByGateId(Long gateId) {
return visitService.getByGateId(gateId);
}
public List<VisitModel> getVisitsByUserId(Long userId) {
return visitService.getByUserId(userId);
}
public Page<VisitModel> getVisitsByCodeIdPaginated(Long codeId, Pageable pageable) {
return visitService.getVisitsByCodeIdPaginated(codeId, pageable);
public Page<VisitModel> getVisitsByGateIdPaginated(Long gateId, Pageable pageable) {
return visitService.getVisitsByGateIdPaginated(gateId, pageable);
}
public Page<VisitModel> getVisitsByUserIdPaginated(Long userId, Pageable pageable) {

View File

@ -1,10 +0,0 @@
package com.displaynone.acss.components.acs.code;
import lombok.Data;
@Data
public class CodeDTO {
private Long id;
private Long codeValue;
}

View File

@ -1,23 +0,0 @@
package com.displaynone.acss.components.acs.code;
import com.displaynone.acss.utils.GlobalUtils;
import lombok.experimental.UtilityClass;
import java.util.Collection;
import java.util.List;
@UtilityClass
public class CodeMapper {
public CodeDTO convertToDTO(CodeModel model) {
CodeDTO dto = new CodeDTO();
dto.setId(model.getId());
dto.setCodeValue(model.getCodeValue());
return dto;
}
public List<CodeDTO> convertAllToDTO(Collection<CodeModel> models) {
return GlobalUtils.convertAllToDTO(models, CodeMapper::convertToDTO);
}
}

View File

@ -1,9 +0,0 @@
package com.displaynone.acss.components.acs.code;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface CodeRepository extends JpaRepository<CodeModel, Long> {
Optional<CodeModel> findByCodeValue(Long codeValue);
}

View File

@ -1,19 +0,0 @@
package com.displaynone.acss.components.acs.code.service;
import com.displaynone.acss.components.acs.code.CodeModel;
import java.util.Optional;
public interface CodeService {
Optional<CodeModel> getCodeById(Long codeId);
CodeModel getCodeByIdStrict(Long codeId);
Optional<CodeModel> getCodeByValue(Long value);
CodeModel getCodeByValueStrict(Long value);
boolean isCodeIdValid(Long codeID);
boolean isValid(Long code);
}

View File

@ -1,50 +0,0 @@
package com.displaynone.acss.components.acs.code.service;
import com.displaynone.acss.components.acs.code.CodeModel;
import com.displaynone.acss.components.acs.code.CodeRepository;
import com.displaynone.acss.exception.generics.NotFoundHTTPException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.Optional;
@Component
@RequiredArgsConstructor
public class CodeServiceImpl implements CodeService {
private final CodeRepository codeRepository;
@Override
public Optional<CodeModel> getCodeById(Long codeId) {
return codeRepository.findById(codeId);
}
@Override
public CodeModel getCodeByIdStrict(Long codeId) {
Optional<CodeModel> model = codeRepository.findById(codeId);
if (model.isEmpty()) throw new NotFoundHTTPException("Code not found");
return model.get();
}
@Override
public Optional<CodeModel> getCodeByValue(Long value) {
return codeRepository.findByCodeValue(value);
}
@Override
public CodeModel getCodeByValueStrict(Long value) {
Optional<CodeModel> model = getCodeByValue(value);
if (model.isEmpty()) throw new NotFoundHTTPException("Code not found");
return model.get();
}
@Override
public boolean isCodeIdValid(Long codeID) {
return getCodeById(codeID).isPresent();
}
@Override
public boolean isValid(Long code) {
return getCodeByValue(code).isPresent();
}
}

View File

@ -0,0 +1,10 @@
package com.displaynone.acss.components.acs.gate;
import lombok.Data;
@Data
public class GateDTO {
private Long id;
private Long code;
}

View File

@ -0,0 +1,23 @@
package com.displaynone.acss.components.acs.gate;
import com.displaynone.acss.utils.GlobalUtils;
import lombok.experimental.UtilityClass;
import java.util.Collection;
import java.util.List;
@UtilityClass
public class GateMapper {
public GateDTO convertToDTO(GateModel model) {
GateDTO dto = new GateDTO();
dto.setId(model.getId());
dto.setCode(model.getCode());
return dto;
}
public List<GateDTO> convertAllToDTO(Collection<GateModel> models) {
return GlobalUtils.convertAllToDTO(models, GateMapper::convertToDTO);
}
}

View File

@ -1,4 +1,4 @@
package com.displaynone.acss.components.acs.code;
package com.displaynone.acss.components.acs.gate;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
@ -9,14 +9,14 @@ import lombok.NoArgsConstructor;
@Entity
@Data
@Builder
@Table(name = "codes")
@Table(name = "gates")
@NoArgsConstructor
@AllArgsConstructor
public class CodeModel {
public class GateModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(nullable = false)
private long codeValue;
private long code;
}

View File

@ -0,0 +1,9 @@
package com.displaynone.acss.components.acs.gate;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface GateRepository extends JpaRepository<GateModel, Long> {
Optional<GateModel> findByCode(Long code);
}

View File

@ -0,0 +1,19 @@
package com.displaynone.acss.components.acs.gate.service;
import com.displaynone.acss.components.acs.gate.GateModel;
import java.util.Optional;
public interface GateService {
Optional<GateModel> getGateById(Long gateId);
GateModel getGateByIdStrict(Long gateId);
Optional<GateModel> getGateByCode(Long code);
GateModel getGateByCodeStrict(Long code);
boolean isGateExists(Long gateId);
boolean isGateCodeValid(Long code);
}

View File

@ -0,0 +1,50 @@
package com.displaynone.acss.components.acs.gate.service;
import com.displaynone.acss.components.acs.gate.GateModel;
import com.displaynone.acss.components.acs.gate.GateRepository;
import com.displaynone.acss.exception.generics.NotFoundHTTPException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.Optional;
@Component
@RequiredArgsConstructor
public class GateServiceImpl implements GateService {
private final GateRepository gateRepository;
@Override
public Optional<GateModel> getGateById(Long gateId) {
return gateRepository.findById(gateId);
}
@Override
public GateModel getGateByIdStrict(Long gateId) {
Optional<GateModel> model = gateRepository.findById(gateId);
if (model.isEmpty()) throw new NotFoundHTTPException("Gate not found");
return model.get();
}
@Override
public Optional<GateModel> getGateByCode(Long code) {
return gateRepository.findByCode(code);
}
@Override
public GateModel getGateByCodeStrict(Long code) {
Optional<GateModel> model = getGateByCode(code);
if (model.isEmpty()) throw new NotFoundHTTPException("Gate not found");
return model.get();
}
@Override
public boolean isGateExists(Long gateId) {
return getGateById(gateId).isPresent();
}
@Override
public boolean isGateCodeValid(Long code) {
return getGateByCode(code).isPresent();
}
}

View File

@ -8,6 +8,6 @@ import java.time.LocalDateTime;
public class VisitDTO {
private Long id;
private Long userId;
private Long codeId;
private Long gateId;
private LocalDateTime createdAt;
}

View File

@ -13,7 +13,7 @@ public class VisitMapper {
dto.setId(model.getId());
dto.setUserId(model.getUserId());
dto.setCodeId(model.getCodeId());
dto.setGateId(model.getGateId());
dto.setCreatedAt(model.getCreatedAt());
return dto;

View File

@ -23,7 +23,7 @@ public class VisitModel {
private Long userId;
@Column(nullable = false)
private Long codeId;
private Long gateId;
@Column(nullable = false)
private LocalDateTime createdAt;

View File

@ -7,11 +7,11 @@ import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface VisitRepository extends JpaRepository<VisitModel, Long> {
List<VisitModel> findByCodeId(Long codeId);
List<VisitModel> findByGateId(Long gateId);
List<VisitModel> findByUserId(Long userId);
Page<VisitModel> findByCodeId(Long codeId, Pageable page);
Page<VisitModel> findByGateId(Long gateId, Pageable page);
Page<VisitModel> findByUserId(Long userId, Pageable page);
}

View File

@ -7,11 +7,11 @@ import org.springframework.data.domain.Pageable;
import java.util.List;
public interface VisitService {
List<VisitModel> getByCodeId(Long code);
List<VisitModel> getByGateId(Long gateId);
List<VisitModel> getByUserId(Long userID);
List<VisitModel> getByUserId(Long userId);
Page<VisitModel> getVisitsByCodeIdPaginated(Long codeId, Pageable pageable);
Page<VisitModel> getVisitsByGateIdPaginated(Long gateId, Pageable pageable);
Page<VisitModel> getVisitsByUserIdPaginated(Long userId, Pageable pageable);
}

View File

@ -15,8 +15,8 @@ public class VisitServiceImpl implements VisitService {
private final VisitRepository visitRepository;
@Override
public List<VisitModel> getByCodeId(Long code) {
return visitRepository.findByCodeId(code);
public List<VisitModel> getByGateId(Long gateId) {
return visitRepository.findByGateId(gateId);
}
@Override
@ -25,8 +25,8 @@ public class VisitServiceImpl implements VisitService {
}
@Override
public Page<VisitModel> getVisitsByCodeIdPaginated(Long codeId, Pageable pageable) {
return visitRepository.findByCodeId(codeId, pageable);
public Page<VisitModel> getVisitsByGateIdPaginated(Long gateId, Pageable pageable) {
return visitRepository.findByGateId(gateId, pageable);
}
@Override

View File

@ -26,7 +26,7 @@ public class ACSController {
@PostMapping("/open")
public ResponseEntity<Void> open(@RequestBody OpenRQB body) {
Long code = body.getCode();
if (!acsComponent.isCodeValid(code)) throw new ForbiddenHTTPException("Invalid code");
if (!acsComponent.isGateCodeValid(code)) throw new ForbiddenHTTPException("Invalid gate code");
return ResponseEntity.ok(null);
}
@ -51,14 +51,14 @@ public class ACSController {
return ResponseEntity.ok(acsComponent.getVisitsByUserIdPaginated(user.getId(), pageable).map(VisitMapper::convertToDTO));
}
@GetMapping("visits/code/{codeId}")
public ResponseEntity<Page<VisitDTO>> getUserVisitsPaginated(
@GetMapping("visits/gate/{gateId}")
public ResponseEntity<Page<VisitDTO>> getGateVisitsPaginated(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size,
@PathVariable Long codeId
@PathVariable Long gateId
) {
Pageable pageable = PageRequest.of(page, size);
if (!acsComponent.isCodeIdValid(codeId)) throw new NotFoundHTTPException("Invalid code id");
return ResponseEntity.ok(acsComponent.getVisitsByCodeIdPaginated(codeId, pageable).map(VisitMapper::convertToDTO));
if (!acsComponent.isGateExists(gateId)) throw new NotFoundHTTPException("Invalid gate id");
return ResponseEntity.ok(acsComponent.getVisitsByGateIdPaginated(gateId, pageable).map(VisitMapper::convertToDTO));
}
}

View File

@ -47,6 +47,9 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
} catch (NotFoundHTTPException e) {
ResponseEntity<ErrorResponse> responseEntity = GlobalExceptionHandler.buildErrorResponse(new UnauthorizedHTTPException(e.getMessage()), HttpStatus.UNAUTHORIZED, null);
return; // TODO
} catch (UnauthorizedHTTPException e) {
ResponseEntity<ErrorResponse> responseEntity = GlobalExceptionHandler.buildErrorResponse(new UnauthorizedHTTPException(e.getMessage()), HttpStatus.UNAUTHORIZED, null);
return; // TODO
}
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));

View File

@ -4,12 +4,12 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<changeSet id="codes" author="universall">
<createTable tableName="codes">
<changeSet id="gates" author="universall">
<createTable tableName="gates">
<column name="id" type="INTEGER" autoIncrement="true">
<constraints primaryKey="true"/>
</column>
<column name="code_value" type="BIGINT">
<column name="code" type="BIGINT">
<constraints unique="true" nullable="false"/>
</column>
</createTable>

View File

@ -3,7 +3,7 @@
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">
<changeSet id="codes-data" author="universall">
<loadData tableName="codes" file="db/changelog/data/csv/0004-codes-data.csv" separator=";" quotchar='"'/>
<changeSet id="gates-data" author="universall">
<loadData tableName="gates" file="db/changelog/data/csv/0004-gates-data.csv" separator=";" quotchar='"'/>
</changeSet>
</databaseChangeLog>

View File

@ -1,4 +1,4 @@
id;code_value
id;code
1;1234567890123456789
2;9223372036854775807
3;1122334455667788990
1 id code_value code
2 1 1234567890123456789 1234567890123456789
3 2 9223372036854775807 9223372036854775807
4 3 1122334455667788990 1122334455667788990

View File

@ -8,10 +8,10 @@
<include file="db/changelog/01/0002-roles.xml"/>
<include file="db/changelog/01/0001-users.xml"/>
<include file="db/changelog/01/0003-user_roles.xml"/>
<include file="db/changelog/01/0004-codes.xml"/>
<include file="db/changelog/01/0004-gates.xml"/>
<include file="db/changelog/data/0002-roles-data.xml"/>
<include file="db/changelog/data/0001-user-data.xml"/>
<include file="db/changelog/data/0003-user_roles-data.xml"/>
<include file="db/changelog/data/0004-codes-data.xml"/>
<include file="db/changelog/data/0004-gates-data.xml"/>
</databaseChangeLog>