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

Implement car models #28

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4,248 changes: 4,248 additions & 0 deletions etc/sql/02-CarModelsInserts.sql

Large diffs are not rendered by default.

This file was deleted.

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 src/main/java/com/example/backend/car/dto/CarModelDto.java
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;
}
}
54 changes: 54 additions & 0 deletions src/main/java/com/example/backend/car/model/CarModel.java
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;
}
}
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 src/main/java/com/example/backend/car/service/CarBrandService.java

This file was deleted.

32 changes: 32 additions & 0 deletions src/main/java/com/example/backend/car/service/CarService.java
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);
}

}