-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
239 additions
and
5 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
inventory/src/main/java/com/fjb/inventory/controller/WarehouseController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.fjb.inventory.controller; | ||
|
||
import com.fjb.inventory.dto.request.WarehouseRequestDto; | ||
import com.fjb.inventory.dto.response.WarehouseResponseDto; | ||
import com.fjb.inventory.service.WarehouseService; | ||
import jakarta.validation.Valid; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/warehouses") | ||
@RequiredArgsConstructor | ||
public class WarehouseController { | ||
private final WarehouseService warehouseService; | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<WarehouseResponseDto> getWarehouse(@PathVariable("id") Long id) { | ||
return ResponseEntity.status(HttpStatus.OK).body(warehouseService.getWarehouseById(id)); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<WarehouseResponseDto> createWarehouse(@RequestBody @Valid | ||
WarehouseRequestDto warehouseRequestDto) { | ||
return ResponseEntity.status(HttpStatus.CREATED).body(warehouseService.createWarehouse(warehouseRequestDto)); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public ResponseEntity<WarehouseResponseDto> updateWarehouse(@PathVariable("id") Long id, | ||
@RequestBody @Valid | ||
WarehouseRequestDto warehouseRequestDto) { | ||
return ResponseEntity.status(HttpStatus.OK).body(warehouseService.updateWarehouse(id, warehouseRequestDto)); | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<List<WarehouseResponseDto>> getAllWarehouses() { | ||
return ResponseEntity.status(HttpStatus.OK).body(warehouseService.getAllWarehouses()); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
private ResponseEntity<Void> deleteWarehouse(@PathVariable("id") Long id) { | ||
warehouseService.deleteWarehouse(id); | ||
return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
inventory/src/main/java/com/fjb/inventory/dto/request/WarehouseRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.fjb.inventory.dto.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class WarehouseRequestDto { | ||
|
||
@NotBlank(message = "Warehouse name not blank") | ||
private String name; | ||
|
||
@NotBlank(message = "Warehouse location not blank") | ||
private String location; | ||
|
||
private String description; | ||
} |
13 changes: 13 additions & 0 deletions
13
inventory/src/main/java/com/fjb/inventory/dto/response/WarehouseResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.fjb.inventory.dto.response; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class WarehouseResponseDto { | ||
private Long id; | ||
private String name; | ||
private String location; | ||
private String description; | ||
} |
29 changes: 29 additions & 0 deletions
29
inventory/src/main/java/com/fjb/inventory/entity/Warehouse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.fjb.inventory.entity; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Entity | ||
@Getter | ||
@Setter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Table(name = "warehouses") | ||
public class Warehouse { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String name; | ||
private String description; | ||
private String location; | ||
} |
19 changes: 19 additions & 0 deletions
19
inventory/src/main/java/com/fjb/inventory/exception/NotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.fjb.inventory.exception; | ||
|
||
public class NotFoundException extends RuntimeException { | ||
public NotFoundException() { | ||
super(); | ||
} | ||
|
||
public NotFoundException(String message) { | ||
super(message); | ||
} | ||
|
||
public NotFoundException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public NotFoundException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
inventory/src/main/java/com/fjb/inventory/mapper/WarehouseMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.fjb.inventory.mapper; | ||
|
||
import com.fjb.inventory.dto.request.WarehouseRequestDto; | ||
import com.fjb.inventory.dto.response.WarehouseResponseDto; | ||
import com.fjb.inventory.entity.Warehouse; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.MappingTarget; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface WarehouseMapper { | ||
Warehouse toWarehouse(WarehouseRequestDto warehouseRequestDto); | ||
|
||
WarehouseResponseDto toWarehouseResponse(Warehouse warehouse); | ||
|
||
Warehouse updateWarehouse(@MappingTarget Warehouse warehouse, WarehouseRequestDto warehouseRequestDto); | ||
} |
7 changes: 7 additions & 0 deletions
7
inventory/src/main/java/com/fjb/inventory/repository/WarehouseRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.fjb.inventory.repository; | ||
|
||
import com.fjb.inventory.entity.Warehouse; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface WarehouseRepository extends JpaRepository<Warehouse, Long> { | ||
} |
17 changes: 17 additions & 0 deletions
17
inventory/src/main/java/com/fjb/inventory/service/WarehouseService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.fjb.inventory.service; | ||
|
||
import com.fjb.inventory.dto.request.WarehouseRequestDto; | ||
import com.fjb.inventory.dto.response.WarehouseResponseDto; | ||
import java.util.List; | ||
|
||
public interface WarehouseService { | ||
WarehouseResponseDto createWarehouse(WarehouseRequestDto warehouseRequestDto); | ||
|
||
WarehouseResponseDto getWarehouseById(Long id); | ||
|
||
WarehouseResponseDto updateWarehouse(Long id, WarehouseRequestDto warehouseRequestDto); | ||
|
||
void deleteWarehouse(Long id); | ||
|
||
List<WarehouseResponseDto> getAllWarehouses(); | ||
} |
59 changes: 59 additions & 0 deletions
59
inventory/src/main/java/com/fjb/inventory/service/impl/WarehouseServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.fjb.inventory.service.impl; | ||
|
||
import com.fjb.inventory.dto.request.WarehouseRequestDto; | ||
import com.fjb.inventory.dto.response.WarehouseResponseDto; | ||
import com.fjb.inventory.entity.Warehouse; | ||
import com.fjb.inventory.exception.NotFoundException; | ||
import com.fjb.inventory.mapper.WarehouseMapper; | ||
import com.fjb.inventory.repository.WarehouseRepository; | ||
import com.fjb.inventory.service.WarehouseService; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class WarehouseServiceImpl implements WarehouseService { | ||
private final WarehouseRepository warehouseRepository; | ||
private final WarehouseMapper warehouseMapper; | ||
|
||
@Override | ||
@Transactional | ||
public WarehouseResponseDto createWarehouse(WarehouseRequestDto warehouseRequestDto) { | ||
Warehouse warehouse = warehouseMapper.toWarehouse(warehouseRequestDto); | ||
return warehouseMapper.toWarehouseResponse(warehouseRepository.save(warehouse)); | ||
} | ||
|
||
@Override | ||
public WarehouseResponseDto getWarehouseById(Long id) { | ||
Warehouse warehouse = warehouseRepository.findById(id) | ||
.orElseThrow(() -> new NotFoundException("Warehouse not found")); | ||
return warehouseMapper.toWarehouseResponse(warehouse); | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public WarehouseResponseDto updateWarehouse(Long id, WarehouseRequestDto warehouseRequestDto) { | ||
Warehouse warehouse = warehouseRepository.findById(id) | ||
.orElseThrow(() -> new NotFoundException("Warehouse not found")); | ||
warehouse = warehouseMapper.updateWarehouse(warehouse, warehouseRequestDto); | ||
return warehouseMapper.toWarehouseResponse(warehouseRepository.save(warehouse)); | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public void deleteWarehouse(Long id) { | ||
Warehouse warehouse = warehouseRepository.findById(id) | ||
.orElseThrow(() -> new NotFoundException("Warehouse not found")); | ||
warehouseRepository.delete(warehouse); | ||
} | ||
|
||
@Override | ||
public List<WarehouseResponseDto> getAllWarehouses() { | ||
return warehouseRepository.findAll() | ||
.stream().map(warehouseMapper::toWarehouseResponse) | ||
.toList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
server.port=8080 | ||
server.port=8081 | ||
server.servlet.context-path=/inventory | ||
|
||
spring.profiles.active=test | ||
|
||
spring.data.mongodb.uri=jdbc:h2:mem:testdb;NON_KEYWORDS=VALUE | ||
spring.datasource.driverClassName=org.h2.Driver | ||
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE | ||
spring.datasource.driver-class-name=org.h2.Driver | ||
spring.datasource.username=sa | ||
spring.datasource.password= | ||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect | ||
|
||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect | ||
spring.jpa.hibernate.ddl-auto=update | ||
spring.liquibase.enabled=false | ||
spring.security.oauth2.resourceserver.jwt.issuer-uri=test | ||
springdoc.oauthflow.authorization-url=test | ||
springdoc.oauthflow.token-url=test |