-
Notifications
You must be signed in to change notification settings - Fork 0
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
8 changed files
with
4,444 additions
and
48 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
src/main/java/com/example/backend/car/controller/CarBrandController.java
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
src/main/java/com/example/backend/car/controller/CarController.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,39 @@ | ||
package com.example.backend.car.controller; | ||
|
||
import com.example.backend.car.dto.CarBrandDto; | ||
import com.example.backend.car.dto.CarModelDto; | ||
import com.example.backend.car.service.CarService; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/api/cars") | ||
public class CarController { | ||
|
||
private final CarService carBrandService; | ||
|
||
public CarController(CarService carBrandService) { | ||
this.carBrandService = carBrandService; | ||
} | ||
|
||
@GetMapping("list") | ||
public List<CarBrandDto> get() { | ||
return carBrandService.list().stream().map((carBrand) -> CarBrandDto.of(carBrand.getId(), carBrand.getName(), carBrand.getSearchKey())).toList(); | ||
} | ||
|
||
@GetMapping("/{brand_id}") | ||
public List<CarModelDto> getModels(@PathVariable("brand_id") Integer brandId) { | ||
return carBrandService.getModels(brandId).stream() | ||
.map(carModel -> CarModelDto.of( | ||
carModel.getId(), | ||
carModel.getName(), | ||
carModel.getSearchKey(), | ||
carModel.getBrand().getId() | ||
)) | ||
.toList(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/com/example/backend/car/dto/CarModelDto.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,58 @@ | ||
package com.example.backend.car.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class CarModelDto { | ||
|
||
private Integer id; | ||
|
||
private String name; | ||
|
||
private String searchKey; | ||
|
||
private Integer brandId; | ||
|
||
public static CarModelDto of(Integer id, String name, String searchKey, Integer brandId) { | ||
CarModelDto carModelDto = new CarModelDto(); | ||
carModelDto.setId(id); | ||
carModelDto.setName(name); | ||
carModelDto.setSearchKey(searchKey); | ||
carModelDto.setBrandId(brandId); | ||
return carModelDto; | ||
} | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getSearchKey() { | ||
return searchKey; | ||
} | ||
|
||
public void setSearchKey(String searchKey) { | ||
this.searchKey = searchKey; | ||
} | ||
|
||
public Integer getBrandId() { | ||
return brandId; | ||
} | ||
|
||
public void setBrandId(Integer brandId) { | ||
this.brandId = brandId; | ||
} | ||
} |
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.example.backend.car.model; | ||
|
||
import jakarta.persistence.*; | ||
|
||
@Entity | ||
@Table(name = "car_model") | ||
public class CarModel { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private int id; | ||
|
||
@Column(length = 64, nullable = false) | ||
private String name; | ||
|
||
@Column(length = 64, nullable = false) | ||
private String searchKey; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "car_brand_id", referencedColumnName = "id", nullable = false) | ||
private CarBrand brand; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getSearchKey() { | ||
return searchKey; | ||
} | ||
|
||
public void setSearchKey(String searchKey) { | ||
this.searchKey = searchKey; | ||
} | ||
|
||
public CarBrand getBrand() { | ||
return brand; | ||
} | ||
|
||
public void setBrand(CarBrand brand) { | ||
this.brand = brand; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/example/backend/car/repository/CarModelRepository.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.example.backend.car.repository; | ||
|
||
import com.example.backend.car.model.CarModel; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CarModelRepository extends JpaRepository<CarModel, Integer> { | ||
|
||
List<CarModel> findAllByBrandIdOrderByNameAsc(Integer brandId); | ||
|
||
} |
22 changes: 0 additions & 22 deletions
22
src/main/java/com/example/backend/car/service/CarBrandService.java
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
src/main/java/com/example/backend/car/service/CarService.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,32 @@ | ||
package com.example.backend.car.service; | ||
|
||
import com.example.backend.car.model.CarBrand; | ||
import com.example.backend.car.model.CarModel; | ||
import com.example.backend.car.repository.CarBrandRepository; | ||
import com.example.backend.car.repository.CarModelRepository; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class CarService { | ||
|
||
private final CarBrandRepository carBrandRepository; | ||
|
||
private final CarModelRepository carModelRepository; | ||
|
||
public CarService(CarBrandRepository carBrandRepository, CarModelRepository carModelRepository) { | ||
this.carBrandRepository = carBrandRepository; | ||
this.carModelRepository = carModelRepository; | ||
} | ||
|
||
public List<CarBrand> list() { | ||
return carBrandRepository.findAllByOrderByNameAsc(); | ||
} | ||
|
||
public List<CarModel> getModels(Integer brandId) { | ||
return carModelRepository.findAllByBrandIdOrderByNameAsc(brandId); | ||
} | ||
|
||
} |