Skip to content

Commit

Permalink
add warehouse inventory (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhduzz authored Oct 22, 2024
1 parent 930126c commit 576cdff
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 5 deletions.
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();
}
}
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;
}
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 inventory/src/main/java/com/fjb/inventory/entity/Warehouse.java
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;
}
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);
}
}
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);
}
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> {
}
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();
}
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();
}
}
12 changes: 7 additions & 5 deletions inventory/src/test/resources/application.properties
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

0 comments on commit 576cdff

Please sign in to comment.