Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/shipper2 #4

Merged
merged 7 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.flab.moduletrucker;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;

@EnableFeignClients
@SpringBootApplication
@ImportAutoConfiguration({FeignAutoConfiguration.class})
public class ModuleTruckerApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.flab.moduletrucker.truck.controller;

import com.flab.moduletrucker.truck.dto.TruckerDTO;
import com.flab.moduletrucker.truck.service.TruckerService;
import com.flab.moduletrucker.truck.service.AccountService;
import com.flab.moduletrucker.truck.service.CarService;
import com.flab.moduletrucker.truck.service.ContractService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -12,36 +14,44 @@
@RequestMapping("/api/v1/trucker")
public class TruckerApiController {

private final TruckerService truckerService;
private final AccountService accountService;
private final CarService carService;
private final ContractService contractService;
Comment on lines +17 to +19
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전 여전히 각각의 서비스들을 컨트롤러에 가져오는 것의 이점을 아직 잘 모르겠어요!! 이 경우에도 특정 URL은 하나의 서비스만 사용하는 것 같기도 하구요. :-)


@PostMapping("/car")
public ResponseEntity<String> createCar(@RequestBody TruckerDTO.CarRequest req) {
String carId = truckerService.createCar(req);
String carId = carService.createCar(req);
return ResponseEntity.status(HttpStatus.OK).body(carId);
}

@GetMapping("/car/{carId}")
public ResponseEntity<TruckerDTO.CarInfo> getCar(@PathVariable String carId) {
TruckerDTO.CarInfo car = truckerService.getCar(carId);
TruckerDTO.CarInfo car = carService.getCar(carId);
return ResponseEntity.status(HttpStatus.OK).body(car);
}

@PostMapping("/contract")
public ResponseEntity<String> createContract(@RequestBody TruckerDTO.ContractRequest req) {
String contractId = truckerService.createContract(req);
String contractId = contractService.createContract(req);
return ResponseEntity.status(HttpStatus.OK).body(contractId);
}

@PostMapping("/account")
public ResponseEntity<String> createAccount(@RequestBody TruckerDTO.AccountRequest req) {
String accountId = truckerService.createAccount(req);
String accountId = accountService.createAccount(req);
return ResponseEntity.status(HttpStatus.OK).body(accountId);
}

@PutMapping("/contract/{contractId}/status/{status}")
public ResponseEntity<String> updateContractStatus(@PathVariable String contractId, @PathVariable String status) {
String resultStatus = truckerService.updateContractStatus(contractId, status);
String resultStatus = contractService.updateContractStatus(contractId, status);
return ResponseEntity.status(HttpStatus.OK).body(resultStatus);
}

@GetMapping("/contract/{contractId}")
public ResponseEntity<TruckerDTO.ContractResponse> getContract(@PathVariable String contractId) {
TruckerDTO.ContractResponse contractResponse = contractService.getContract(contractId);
return ResponseEntity.status(HttpStatus.OK).body(contractResponse);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,22 @@ public Account dtoToDomain(TruckerDTO.AccountRequest req) {
}
}

@Data
@NoArgsConstructor
public static class ContractResponse {
private String contractId;
private String truckerMemberId;
private String shipmentId;
private String carId;
private String contractStatus;

public ContractResponse(Contract contract) {
this.contractId = contract.getContractId();
this.truckerMemberId = contract.getTruckerMemberId();
this.shipmentId = contract.getShipmentId();
this.carId = contract.getCarId();
this.contractStatus = contract.getContractStatus();
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.flab.moduletrucker.truck.repository;

import com.flab.moduletrucker.truck.domain.Account;
import org.springframework.data.jpa.repository.JpaRepository;

public interface AccountRepository {
Account save(Account account);
public interface AccountRepository extends JpaRepository<Account, String> {
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.flab.moduletrucker.truck.repository;

import com.flab.moduletrucker.truck.domain.Car;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface CarRepository {

Car save(Car car);

Optional<Car> findById(String carId);
public interface CarRepository extends JpaRepository<Car, String> {
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.flab.moduletrucker.truck.repository;

import com.flab.moduletrucker.truck.domain.Contract;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;
public interface ContractRepository extends JpaRepository<Contract, String> {

public interface ContractRepository {
Contract save(Contract contract);

Optional<Contract> findById(String contractId);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.flab.moduletrucker.truck.service;

import com.flab.moduletrucker.truck.domain.Account;
import com.flab.moduletrucker.truck.dto.TruckerDTO;
import com.flab.moduletrucker.truck.repository.AccountRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class AccountService {

private final AccountRepository accountRepository;

public String createAccount(TruckerDTO.AccountRequest req) {
Account account = req.dtoToDomain(req);
Account save = accountRepository.save(account);
return save.getAccountId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.flab.moduletrucker.truck.service;

import com.flab.moduletrucker.truck.domain.Car;
import com.flab.moduletrucker.truck.dto.TruckerDTO;
import com.flab.moduletrucker.truck.repository.CarRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Slf4j
@Service
@RequiredArgsConstructor
public class CarService {

private final CarRepository carRepository;

public String createCar(TruckerDTO.CarRequest req) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public String createCar(TruckerDTO.CarRequest req) {
String car = createCar(...);

Car car = req.dtoToDomain(req);
Car save = carRepository.save(car);
return save.getCarId();
}

public TruckerDTO.CarInfo getCar(String carId) {
Optional<Car> byId = carRepository.findById(carId);
if (byId.isPresent()) {
Car car = byId.get();
return new TruckerDTO.CarInfo(car);
} else {
throw new RuntimeException("car not found");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package com.flab.moduletrucker.truck.service;

import com.flab.moduletrucker.shipment.dto.ShipmentDTO;
import com.flab.moduletrucker.truck.domain.Account;
import com.flab.moduletrucker.truck.domain.Car;
import com.flab.moduletrucker.truck.domain.Contract;
import com.flab.moduletrucker.truck.dto.TruckerDTO;
import com.flab.moduletrucker.truck.feign.ShipperClient;
import com.flab.moduletrucker.truck.repository.AccountRepository;
import com.flab.moduletrucker.truck.repository.CarRepository;
import com.flab.moduletrucker.truck.repository.ContractRepository;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
Expand All @@ -20,20 +15,11 @@
@Slf4j
@Service
@RequiredArgsConstructor
@Builder
public class TruckerService {
public class ContractService {

private final CarRepository carRepository;
private final ContractRepository contractRepository;
private final AccountRepository accountRepository;
private final ShipperClient shipperClient;

public String createCar(TruckerDTO.CarRequest req) {
Car car = req.dtoToDomain(req);
Car save = carRepository.save(car);
return save.getCarId();
}

public String createContract(TruckerDTO.ContractRequest req) {
ResponseEntity<ShipmentDTO.BasicInfo> basicInfoResponseEntity = shipperClient.get(req.getShipmentId());
ShipmentDTO.BasicInfo basicInfo = basicInfoResponseEntity.getBody();
Expand All @@ -49,28 +35,21 @@ public String createContract(TruckerDTO.ContractRequest req) {
}
}

public String createAccount(TruckerDTO.AccountRequest req) {
Account account = req.dtoToDomain(req);
Account save = accountRepository.save(account);
return save.getAccountId();
}

public TruckerDTO.CarInfo getCar(String carId) {
Optional<Car> byId = carRepository.findById(carId);
public String updateContractStatus(String contractId, String status) {
Optional<Contract> byId = contractRepository.findById(contractId);
if (byId.isPresent()) {
Car car = byId.get();
return new TruckerDTO.CarInfo(car);
Contract contract = byId.get();
contract.changeStatus(status);
return status;
} else {
throw new RuntimeException("car not found");
throw new RuntimeException("contract not found");
}
}

public String updateContractStatus(String contractId, String status) {
public TruckerDTO.ContractResponse getContract(String contractId) {
Optional<Contract> byId = contractRepository.findById(contractId);
if (byId.isPresent()) {
Contract contract = byId.get();
contract.changeStatus(status);
return status;
return new TruckerDTO.ContractResponse(byId.get());
} else {
throw new RuntimeException("contract not found");
}
Expand Down
8 changes: 7 additions & 1 deletion module-trucker/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
spring.application.name=module-trucker
server.port=8002
server.port=8002

# mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/truck_matching?serverTimezone=UTC&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123963

This file was deleted.

Loading
Loading