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

일렉 기타 매물 생성 API 구현 #51

Merged
merged 6 commits into from
Feb 27, 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
22 changes: 22 additions & 0 deletions src/main/java/com/ajou/hertz/common/dto/AddressDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ajou.hertz.common.dto;

import com.ajou.hertz.common.entity.Address;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class AddressDto {

private String sido;
private String sgg;
private String emd;

public static AddressDto from(Address address) {
return new AddressDto(address.getSido(), address.getSgg(), address.getEmd());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ajou.hertz.common.dto.request;

import com.ajou.hertz.common.entity.Address;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Setter
@Getter
public class AddressRequest {

@Schema(description = "시/도", example = "서울특별시")
@NotBlank
private String sido;

@Schema(description = "시/군/구", example = "강남구")
@NotBlank
private String sgg;

@Schema(description = "읍/면/동", example = "청담동")
@NotBlank
private String emd;

public Address toEntity() {
return new Address(sido, sgg, emd);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.ajou.hertz.common.dto.response;

import com.ajou.hertz.common.dto.AddressDto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class AddressResponse {

@Schema(description = "시/도", example = "서울특별시")
private String sido;

@Schema(description = "시/군/구", example = "강남구")
private String sgg;

@Schema(description = "읍/면/동", example = "청담동")
private String emd;

public static AddressResponse from(AddressDto addressDto) {
return new AddressResponse(addressDto.getSido(), addressDto.getSgg(), addressDto.getEmd());
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/ajou/hertz/common/entity/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Embeddable
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.ajou.hertz.domain.instrument.controller;

import static com.ajou.hertz.common.constant.GlobalConstants.*;

import java.net.URI;

import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ajou.hertz.common.auth.UserPrincipal;
import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto;
import com.ajou.hertz.domain.instrument.dto.request.CreateNewElectricGuitarRequest;
import com.ajou.hertz.domain.instrument.dto.response.ElectricGuitarResponse;
import com.ajou.hertz.domain.instrument.service.InstrumentCommandService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

@Tag(name = "중고 악기 관련 API")
@RequiredArgsConstructor
@RequestMapping("/v1/instruments")
@RestController
public class InstrumentControllerV1 {

private final InstrumentCommandService instrumentCommandService;

@Operation(
summary = "일렉기타 매물 등록",
description = """
<p>일렉기타 매물을 등록합니다.
<p>요청 시 <strong>multipart/form-data</strong> content-type으로 요쳥해야 합니다.
"""
)
@PostMapping(
value = "/electric-guitars",
headers = API_MINOR_VERSION_HEADER_NAME + "=" + 1,
consumes = MediaType.MULTIPART_FORM_DATA_VALUE
)
public ResponseEntity<ElectricGuitarResponse> createNewInstrumentV1_1(
@AuthenticationPrincipal UserPrincipal userPrincipal,
@ParameterObject @ModelAttribute @Valid CreateNewElectricGuitarRequest createNewElectricGuitarRequest
) {
ElectricGuitarDto electricGuitar = instrumentCommandService.createNewElectricGuitar(
userPrincipal.getUserId(),
createNewElectricGuitarRequest
);
return ResponseEntity
.created(URI.create("/instruments/" + electricGuitar.getId()))
.body(ElectricGuitarResponse.from(electricGuitar));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.ajou.hertz.domain.instrument.dto;

import java.util.List;

import com.ajou.hertz.common.dto.AddressDto;
import com.ajou.hertz.domain.instrument.constant.ElectricGuitarBrand;
import com.ajou.hertz.domain.instrument.constant.ElectricGuitarModel;
import com.ajou.hertz.domain.instrument.constant.GuitarColor;
import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus;
import com.ajou.hertz.domain.instrument.entity.ElectricGuitar;
import com.ajou.hertz.domain.user.dto.UserDto;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class ElectricGuitarDto {

private Long id;
private UserDto seller;
private String title;
private InstrumentProgressStatus progressStatus;
private AddressDto tradeAddress;
private Short qualityStatus;
private Integer price;
private Boolean hasAnomaly;
private String description;
private ElectricGuitarBrand brand;
private ElectricGuitarModel model;
private Short productionYear;
private GuitarColor color;
private List<InstrumentImageDto> images;
private List<String> hashtags;

public static ElectricGuitarDto from(ElectricGuitar electricGuitar) {
return new ElectricGuitarDto(
electricGuitar.getId(),
UserDto.from(electricGuitar.getSeller()),
electricGuitar.getTitle(),
electricGuitar.getProgressStatus(),
AddressDto.from(electricGuitar.getTradeAddress()),
electricGuitar.getQualityStatus(),
electricGuitar.getPrice(),
electricGuitar.getHasAnomaly(),
electricGuitar.getDescription(),
electricGuitar.getBrand(),
electricGuitar.getModel(),
electricGuitar.getProductionYear(),
electricGuitar.getColor(),
electricGuitar.getImages().toDtos(),
electricGuitar.getHashtags().toStrings()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ajou.hertz.domain.instrument.dto;

import com.ajou.hertz.domain.instrument.entity.InstrumentImage;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class InstrumentImageDto {

private Long id;
private String name;
private String url;

public static InstrumentImageDto from(InstrumentImage instrumentImage) {
return new InstrumentImageDto(
instrumentImage.getId(),
instrumentImage.getOriginalName(),
instrumentImage.getUrl()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.ajou.hertz.domain.instrument.dto.request;

import java.util.List;

import org.hibernate.validator.constraints.Length;
import org.springframework.web.multipart.MultipartFile;

import com.ajou.hertz.common.dto.request.AddressRequest;
import com.ajou.hertz.domain.instrument.constant.ElectricGuitarBrand;
import com.ajou.hertz.domain.instrument.constant.ElectricGuitarModel;
import com.ajou.hertz.domain.instrument.constant.GuitarColor;
import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus;
import com.ajou.hertz.domain.instrument.entity.ElectricGuitar;
import com.ajou.hertz.domain.user.entity.User;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Setter // for multipart/form-data with @ModelAttribute
@Getter
public class CreateNewElectricGuitarRequest {

@Schema(description = "제목", example = "펜더 로드원 텔레캐스터")
@NotBlank
private String title;

@Schema(description = "악기 사진")
@NotNull
@Size(min = 4, max = 7)
private List<MultipartFile> images;

@NotNull
private InstrumentProgressStatus progressStatus;

@Schema(description = "거래 장소")
@NotNull
private AddressRequest tradeAddress;

@Schema(description = "악기 상태. 1~5 단계로 구성됩니다.", example = "3")
@NotNull
@Min(1)
@Max(5)
private Short qualityStatus;

@Schema(description = "가격", example = "527000")
@NotNull
private Integer price;

@Schema(description = "특이사항 유무", example = "true")
@NotNull
private Boolean hasAnomaly;

@Schema(description = "특이사항 및 상세 설명. 내용이 없을 경우에는 빈 문자열로 요청하면 됩니다.", example = "14년 시리얼 펜더 로드원 50 텔레입니다. 기존 ...")
@NotNull
private String description;

@NotNull
private ElectricGuitarBrand brand;

@NotNull
private ElectricGuitarModel model;

@Schema(description = "생상연도", example = "2014")
@NotNull
private Short productionYear;

@NotNull
private GuitarColor color;

@Schema(description = "해시태그(각 해시태그마다 최대 10글자)", example = "[\"펜더\", \"Fender\"]")
private List<@NotBlank @Length(max = 10) String> hashtags;

public ElectricGuitar toEntity(User seller) {
return ElectricGuitar.create(
seller,
title,
progressStatus,
tradeAddress.toEntity(),
qualityStatus,
price,
hasAnomaly,
description,
brand,
model,
productionYear,
color
);
}
}
Loading
Loading