-
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.
Merge pull request #38 from SSUMC-6th/suffle_#30
[suffle] Chapter09_API & Swagger & Annotation
- Loading branch information
Showing
39 changed files
with
957 additions
and
2 deletions.
There are no files selected for viewing
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
82 changes: 82 additions & 0 deletions
82
src/mission/umc/src/main/java/com/example/umc/common/configuration/SwaggerConfiguration.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,82 @@ | ||
package com.example.umc.common.configuration; | ||
|
||
import io.swagger.v3.oas.annotations.OpenAPIDefinition; | ||
import io.swagger.v3.oas.models.Components; | ||
import io.swagger.v3.oas.models.ExternalDocumentation; | ||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.info.Info; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
import io.swagger.v3.oas.models.security.SecurityRequirement; | ||
import io.swagger.v3.oas.models.security.SecurityScheme; | ||
import io.swagger.v3.oas.models.servers.Server; | ||
import org.springdoc.core.utils.SpringDocUtils; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.awt.*; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Collections; | ||
|
||
@Configuration | ||
@OpenAPIDefinition | ||
public class SwaggerConfiguration { | ||
|
||
private final SecurityScheme securityScheme = new SecurityScheme() | ||
.type(SecurityScheme.Type.APIKEY) | ||
.in(SecurityScheme.In.HEADER) | ||
.name("Authorization"); | ||
|
||
{ | ||
SpringDocUtils.getConfig().replaceWithSchema(Color.class, | ||
new Schema<String>() | ||
.type("string") | ||
.format("color") | ||
.example("#FFFFFFFF")); | ||
|
||
SpringDocUtils.getConfig().replaceWithSchema(LocalDateTime.class, | ||
new Schema<LocalDateTime>() | ||
.type("string") | ||
.format("date-time") | ||
.example(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))); | ||
|
||
SpringDocUtils.getConfig().replaceWithSchema(LocalDate.class, | ||
new Schema<LocalDate>() | ||
.type("string") | ||
.format("date") | ||
.example(LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE))); | ||
|
||
SpringDocUtils.getConfig().replaceWithSchema(LocalTime.class, | ||
new Schema<LocalTime>() | ||
.type("string") | ||
.format("time") | ||
.example(LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")))); | ||
} | ||
|
||
@Bean | ||
public OpenAPI openApi() { | ||
String description = "umc Swagger setting"; | ||
String securityRequirementName = "bearerAuth"; | ||
|
||
// 파일 업로드를 위한 스웨거 정의 추가 | ||
Schema fileSchema = new Schema(); | ||
fileSchema.setType("string"); | ||
fileSchema.setFormat("binary"); | ||
|
||
return new OpenAPI() | ||
.servers(Collections.singletonList(new Server().url("/"))) | ||
.security(Collections.singletonList(new SecurityRequirement().addList(securityRequirementName))) | ||
.components(new Components() | ||
.addSecuritySchemes(securityRequirementName, securityScheme) | ||
.addSchemas("file", fileSchema)) //파일 스키마 추가 | ||
.info(new Info() | ||
.title("API") | ||
.description(description) | ||
.version("0.0.1") | ||
) | ||
.externalDocs(new ExternalDocumentation().description("team-m API")); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/mission/umc/src/main/java/com/example/umc/domain/Loader/DataLoader.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,42 @@ | ||
package com.example.umc.domain.Loader; | ||
|
||
import com.example.umc.domain.Member; | ||
import com.example.umc.domain.Region; | ||
import com.example.umc.domain.enums.Gender; | ||
import com.example.umc.domain.enums.MemberStatus; | ||
import com.example.umc.domain.enums.SocialType; | ||
import com.example.umc.repository.MemberRepository; | ||
import com.example.umc.repository.RegionRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class DataLoader implements CommandLineRunner { | ||
|
||
private final MemberRepository memberRepository; | ||
private final RegionRepository regionRepository; | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
Member member = Member.builder() | ||
.name("John Doe") | ||
.address("123 Main St") | ||
.specAddress("Apt 4B") | ||
.gender(Gender.MALE) | ||
.socialType(SocialType.KAKAO) // Assuming SocialType.FACEBOOK is a valid value | ||
.status(MemberStatus.ACTIVE) | ||
.email("[email protected]") | ||
.point(100) | ||
.build(); | ||
|
||
memberRepository.save(member); | ||
|
||
Region region = Region.builder() | ||
.name("Seoul") | ||
.build(); | ||
regionRepository.save(region); | ||
} | ||
} |
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
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
3 changes: 3 additions & 0 deletions
3
src/mission/umc/src/main/java/com/example/umc/domain/enums/MissionStatus.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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
package com.example.umc.domain.enums; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum MissionStatus { | ||
CHALLENGING, COMPLETE | ||
} |
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
8 changes: 8 additions & 0 deletions
8
src/mission/umc/src/main/java/com/example/umc/repository/FoodCategoryRepository.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,8 @@ | ||
package com.example.umc.repository; | ||
|
||
import com.example.umc.domain.FoodCategory; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface FoodCategoryRepository extends JpaRepository<FoodCategory, Long> { | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/mission/umc/src/main/java/com/example/umc/repository/MemberMissionRepository.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,7 @@ | ||
package com.example.umc.repository; | ||
|
||
import com.example.umc.domain.maaping.MemberMission; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface MemberMissionRepository extends JpaRepository<MemberMission, Long> { | ||
} |
8 changes: 8 additions & 0 deletions
8
src/mission/umc/src/main/java/com/example/umc/repository/MemberRepository.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,8 @@ | ||
package com.example.umc.repository; | ||
|
||
import com.example.umc.domain.Member; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface MemberRepository extends JpaRepository<Member, Long> { | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/mission/umc/src/main/java/com/example/umc/repository/MissionRepository.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,8 @@ | ||
package com.example.umc.repository; | ||
|
||
import com.example.umc.domain.Mission; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface MissionRepository extends JpaRepository<Mission, Long> { | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/mission/umc/src/main/java/com/example/umc/repository/RegionRepository.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,8 @@ | ||
package com.example.umc.repository; | ||
|
||
import com.example.umc.domain.Region; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface RegionRepository extends JpaRepository<Region, Long> { | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/mission/umc/src/main/java/com/example/umc/repository/ReviewRepository.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,8 @@ | ||
package com.example.umc.repository; | ||
|
||
import com.example.umc.domain.Review; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ReviewRepository extends JpaRepository<Review, Long> { | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/mission/umc/src/main/java/com/example/umc/repository/StoreRepository.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,7 @@ | ||
package com.example.umc.repository; | ||
|
||
import com.example.umc.domain.Store; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface StoreRepository extends JpaRepository<Store, Long> { | ||
} |
38 changes: 38 additions & 0 deletions
38
src/mission/umc/src/main/java/com/example/umc/service/MemberService.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,38 @@ | ||
package com.example.umc.service; | ||
|
||
import com.example.umc.domain.Member; | ||
import com.example.umc.domain.Mission; | ||
import com.example.umc.domain.enums.MissionStatus; | ||
import com.example.umc.domain.maaping.MemberMission; | ||
import com.example.umc.repository.MemberMissionRepository; | ||
import com.example.umc.repository.MemberRepository; | ||
import com.example.umc.repository.MissionRepository; | ||
import com.example.umc.validation.annotation.ExistMission; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.convert.ReadingConverter; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDate; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MemberService { | ||
|
||
private final MemberRepository memberRepository; | ||
private final MissionRepository missionRepository; | ||
private final MemberMissionRepository memberMissionRepository; | ||
|
||
public void addMission(Long missionId, Long memberId) { | ||
Mission mission = missionRepository.getReferenceById(missionId); | ||
Member member = memberRepository.findById(memberId).orElseThrow(()-> new IllegalArgumentException("Member not found")); | ||
|
||
MemberMission memberMission = MemberMission.builder() | ||
.status(MissionStatus.CHALLENGING) | ||
.member(member) | ||
.mission(mission) | ||
.build(); | ||
|
||
member.getMemberMissionList().add(memberMission); | ||
memberMissionRepository.save(memberMission); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/mission/umc/src/main/java/com/example/umc/service/MissionService.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,26 @@ | ||
package com.example.umc.service; | ||
|
||
import com.example.umc.domain.Mission; | ||
import com.example.umc.domain.Store; | ||
import com.example.umc.repository.MissionRepository; | ||
import com.example.umc.repository.StoreRepository; | ||
import com.example.umc.web.dto.request.CreateMissionRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MissionService { | ||
|
||
private final StoreRepository storeRepository; | ||
private final MissionRepository missionRepository; | ||
|
||
public void addMission(CreateMissionRequest createMissionRequest) { | ||
|
||
Store store = storeRepository.getReferenceById(createMissionRequest.getStoreId()); | ||
missionRepository.save(CreateMissionRequest.toEntity(createMissionRequest, store)); | ||
|
||
return; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/mission/umc/src/main/java/com/example/umc/service/ReviewService.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,38 @@ | ||
package com.example.umc.service; | ||
|
||
import com.example.umc.domain.Member; | ||
import com.example.umc.domain.Review; | ||
import com.example.umc.domain.Store; | ||
import com.example.umc.repository.MemberRepository; | ||
import com.example.umc.repository.ReviewRepository; | ||
import com.example.umc.repository.StoreRepository; | ||
import com.example.umc.web.dto.reponse.ReviewResponseDto; | ||
import com.example.umc.web.dto.request.CreateReviewRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class ReviewService { | ||
|
||
private final ReviewRepository reviewRepository; | ||
private final MemberRepository memberRepository; | ||
private final StoreRepository storeRepository; | ||
|
||
public ReviewResponseDto addReview(CreateReviewRequest createReviewRequest) { | ||
Member member = memberRepository.findById(createReviewRequest.getMemberId()) | ||
.orElseThrow(() -> new IllegalArgumentException("Member not found")); | ||
|
||
/* Store store = storeRepository.findById(createReviewRequest.getStoreId()) | ||
.orElseThrow(() -> new IllegalArgumentException("Store not found")); | ||
이렇게 처리를 할 필요가 없는 거지 이미 유효성 검사를 했기 때문에 | ||
*/ | ||
Store store = storeRepository.getReferenceById(createReviewRequest.getStoreId()); | ||
Review review = CreateReviewRequest.toEntity(createReviewRequest, member, store); | ||
Review savedReview = reviewRepository.save(review); | ||
|
||
return ReviewResponseDto.fromEntity(savedReview); | ||
} | ||
} |
Oops, something went wrong.