Skip to content

Commit

Permalink
feat : 어드민 show CRUD 구현 (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaBaljaintheroom authored Jul 20, 2024
1 parent ca93202 commit d1264e1
Show file tree
Hide file tree
Showing 78 changed files with 2,116 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ gradle-app.setting
### QClass ###
**/src/main/generated/

### application-cloud-local.yml
app/src/main/resources/application-cloud-local.yml

# End of https://www.toptal.com/developers/gitignore/api/java,intellij+all,macos,gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
configurer -> configurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
)
.authorizeHttpRequests(registry -> registry
.requestMatchers(notRequireAuthenticationMatcher())
.requestMatchers(getMatcherForAnyone())
.permitAll()
.requestMatchers(requireUserAndAdminAuthenticationMatcher())
.requestMatchers(getMatcherForUserAndAdmin())
.hasAnyRole("USER", "ADMIN")
.anyRequest()
.hasAnyRole("ADMIN")
Expand All @@ -60,14 +60,15 @@ private CorsConfigurationSource corsConfigurationSource() {
};
}

private RequestMatcher notRequireAuthenticationMatcher() {
private RequestMatcher getMatcherForAnyone() {
return RequestMatchers.anyOf(
antMatcher("/swagger-ui/**"),
antMatcher("/v3/api-docs/**"),
antMatcher("/admin/**"),
antMatcher("/css/**"),
antMatcher("/js/**"),
antMatcher(HttpMethod.POST, "/api/v1/users/login"),
antMatcher(HttpMethod.POST, "/admin/login"),
antMatcher(HttpMethod.POST, "/admin/signup"),
antMatcher(HttpMethod.GET, "/admin/home"),
antMatcher(HttpMethod.GET, "/api/v1/artists"),
Expand All @@ -76,7 +77,7 @@ private RequestMatcher notRequireAuthenticationMatcher() {
);
}

private RequestMatcher requireUserAndAdminAuthenticationMatcher() {
private RequestMatcher getMatcherForUserAndAdmin() {
return RequestMatchers.anyOf(
antMatcher(HttpMethod.GET, "/api/v1/shows/interests"),
antMatcher(HttpMethod.POST, "/api/v1/users/logout"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.swagger.v3.oas.models.info.License;
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.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -19,6 +20,7 @@ public class SwaggerConfig {
public OpenAPI openAPI() {
return new OpenAPI()
.info(getInfo())
.addServersItem(getCurrentServerUrl())
.addSecurityItem(getSecurityRequirement())
.components(
new Components().addSecuritySchemes(
Expand All @@ -35,6 +37,10 @@ private Info getInfo() {
.license(getLicense());
}

private Server getCurrentServerUrl() {
return new Server().url("/");
}

private SecurityRequirement getSecurityRequirement() {
return new SecurityRequirement().addList(securitySchemeName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.example.artist.controller.dto.response.ArtistDetailApiFormResponse;
import com.example.artist.service.ArtistAdminService;
import com.example.artist.service.dto.response.ArtistDetailServiceResponse;
import com.example.genre.controller.dto.response.GenreNameApiFormResponse;
import com.example.genre.service.GenreAdminService;
import com.example.genre.service.dto.response.GenreNameServiceResponse;
import jakarta.validation.Valid;
import java.util.List;
import java.util.UUID;
Expand All @@ -30,7 +30,9 @@ public class ArtistAdminController {

@GetMapping
public String createArtist(Model model) {
List<GenreNameServiceResponse> genres = genreAdminService.findAllGenres();
List<GenreNameApiFormResponse> genres = genreAdminService.findAllGenres().stream()
.map(GenreNameApiFormResponse::new)
.toList();
model.addAttribute("genres", genres);
return "artist_create_form";
}
Expand All @@ -43,7 +45,7 @@ public String createArtist(@Valid ArtistCreateApiForm artistCreateApiForm) {

@GetMapping("/list")
public String findAllArtist(Model model) {
List<ArtistDetailApiFormResponse> artistDetailApiFormResponses = artistAdminService.findAllArtist()
List<ArtistDetailApiFormResponse> artistDetailApiFormResponses = artistAdminService.findAllWithGenreNames()
.stream()
.map(ArtistDetailApiFormResponse::new)
.toList();
Expand All @@ -54,7 +56,9 @@ public String findAllArtist(Model model) {

@GetMapping("/{id}")
public String findArtist(@PathVariable("id") UUID id, Model model) {
List<GenreNameServiceResponse> genres = genreAdminService.findAllGenres();
List<GenreNameApiFormResponse> genres = genreAdminService.findAllGenres().stream()
.map(GenreNameApiFormResponse::new)
.toList();
model.addAttribute("genres", genres);

ArtistDetailServiceResponse artistDetailServiceResponse = artistAdminService.findArtistById(
Expand All @@ -67,8 +71,10 @@ public String findArtist(@PathVariable("id") UUID id, Model model) {
}

@PutMapping("/{id}")
public String updateArtist(@PathVariable("id") UUID id,
@Valid ArtistUpdateApiForm artistUpdateApiForm) {
public String updateArtist(
@PathVariable("id") UUID id,
@Valid ArtistUpdateApiForm artistUpdateApiForm
) {
artistAdminService.updateArtist(id, artistUpdateApiForm.toArtistUpdateServiceRequest());
return "redirect:/admin/artists/list";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.UUID;
import org.springframework.web.multipart.MultipartFile;

public record ArtistCreateApiForm(
@NotBlank(message = "아티스트의 한국 이름은 필수 요청값 입니다.")
Expand All @@ -15,6 +16,9 @@ public record ArtistCreateApiForm(
@NotBlank(message = "아티스트의 영어 이름은 필수 요청값 입니다.")
String englishName,

@NotNull(message = "아티스트의 이미지는 필수 요청값 입니다.")
MultipartFile image,

@NotBlank(message = "아티스트의 국적은 필수 요청값 입니다.")
String country,

Expand All @@ -32,6 +36,7 @@ public ArtistCreateServiceRequest toArtistCreateServiceRequest() {
return ArtistCreateServiceRequest.builder()
.koreanName(koreanName)
.englishName(englishName)
.image(image)
.country(country)
.artistGenderApiType(artistGenderApiType)
.artistApiType(artistApiType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.UUID;
import org.springframework.web.multipart.MultipartFile;

public record ArtistUpdateApiForm(
@NotBlank(message = "아티스트의 한국 이름은 필수 요청값 입니다.")
Expand All @@ -15,6 +16,9 @@ public record ArtistUpdateApiForm(
@NotBlank(message = "아티스트의 영어 이름은 필수 요청값 입니다.")
String englishName,

@NotNull(message = "아티스트의 이미지는 필수 요청값 입니다.")
MultipartFile image,

@NotBlank(message = "아티스트의 국적은 필수 요청값 입니다.")
String country,

Expand All @@ -32,6 +36,7 @@ public ArtistUpdateServiceRequest toArtistUpdateServiceRequest() {
return ArtistUpdateServiceRequest.builder()
.koreanName(koreanName)
.englishName(englishName)
.image(image)
.country(country)
.artistGenderApiType(artistGenderApiType)
.artistApiType(artistApiType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public record ArtistDetailApiFormResponse(
UUID id,
String koreanName,
String englishName,
String image,
String country,
ArtistGenderApiType artistGenderApiType,
ArtistApiType artistApiType,
Expand All @@ -24,6 +25,7 @@ public ArtistDetailApiFormResponse(
artistDetailServiceResponse.id(),
artistDetailServiceResponse.koreanName(),
artistDetailServiceResponse.englishName(),
artistDetailServiceResponse.image(),
artistDetailServiceResponse.country(),
artistDetailServiceResponse.artistGenderApiType(),
artistDetailServiceResponse.artistApiType(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.artist.controller.dto.response;

import com.example.artist.service.dto.response.ArtistKoreanNameServiceResponse;
import java.util.UUID;

public record ArtistKoreanNameApiResponse(
UUID id,
String koreanName
) {

public ArtistKoreanNameApiResponse(
ArtistKoreanNameServiceResponse artistKoreanNameServiceResponse) {
this(
artistKoreanNameServiceResponse.id(),
artistKoreanNameServiceResponse.koreanName()
);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import com.example.artist.service.dto.request.ArtistCreateServiceRequest;
import com.example.artist.service.dto.request.ArtistUpdateServiceRequest;
import com.example.artist.service.dto.response.ArtistDetailServiceResponse;
import com.example.artist.service.dto.response.ArtistKoreanNameServiceResponse;
import com.example.component.FileUploadComponent;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.example.dto.artist.response.ArtistDetailResponse;
import org.example.dto.artist.response.ArtistKoreanNameResponse;
import org.example.entity.artist.Artist;
import org.example.usecase.artist.ArtistUseCase;
import org.springframework.stereotype.Service;
Expand All @@ -16,25 +19,37 @@
public class ArtistAdminService {

private final ArtistUseCase artistUseCase;
private final FileUploadComponent fileUploadComponent;

public void save(ArtistCreateServiceRequest artistCreateServiceRequest) {
Artist artist = artistCreateServiceRequest.toArtist();
String imageUrl = fileUploadComponent.uploadFile("artist", artistCreateServiceRequest.image());

Artist artist = artistCreateServiceRequest.toArtistWithImageUrl(imageUrl);
artistUseCase.save(artist, artistCreateServiceRequest.genreIds());
}

public List<ArtistDetailServiceResponse> findAllArtist() {
public List<ArtistDetailServiceResponse> findAllWithGenreNames() {
List<ArtistDetailResponse> artistDetailResponses = artistUseCase.findAllWithGenreNames();
return artistDetailResponses.stream()
.map(ArtistDetailServiceResponse::new)
.toList();
}

public List<ArtistKoreanNameServiceResponse> findAllArtistKoreanName() {
List<ArtistKoreanNameResponse> artistKoreanNameResponses = artistUseCase.findAllArtistKoreanName();
return artistKoreanNameResponses.stream()
.map(ArtistKoreanNameServiceResponse::new)
.toList();
}

public ArtistDetailServiceResponse findArtistById(UUID id) {
return new ArtistDetailServiceResponse(artistUseCase.findArtistDetailById(id));
}

public void updateArtist(UUID id, ArtistUpdateServiceRequest artistUpdateServiceRequest) {
Artist artist = artistUpdateServiceRequest.toArtist();
String imageUrl = fileUploadComponent.uploadFile("artist", artistUpdateServiceRequest.image());

Artist artist = artistUpdateServiceRequest.toArtist(imageUrl);
artistUseCase.updateArtist(id, artist, artistUpdateServiceRequest.genreIds());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@
import org.example.entity.artist.Artist;
import org.example.entity.artist.ArtistGender;
import org.example.entity.artist.ArtistType;
import org.springframework.web.multipart.MultipartFile;

@Builder
public record ArtistCreateServiceRequest(
String koreanName,
String englishName,
MultipartFile image,
String country,
ArtistGenderApiType artistGenderApiType,
ArtistApiType artistApiType,
List<UUID> genreIds
) {

public Artist toArtist() {
public Artist toArtistWithImageUrl(String imageUrl) {
return Artist.builder()
.koreanName(koreanName)
.englishName(englishName)
.image(imageUrl)
.country(country)
.artistGender(ArtistGender.valueOf(artistGenderApiType.name()))
.artistType(ArtistType.valueOf(artistApiType.name()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@
import org.example.entity.artist.Artist;
import org.example.entity.artist.ArtistGender;
import org.example.entity.artist.ArtistType;
import org.springframework.web.multipart.MultipartFile;

@Builder
public record ArtistUpdateServiceRequest(
String koreanName,
String englishName,
MultipartFile image,
String country,
ArtistGenderApiType artistGenderApiType,
ArtistApiType artistApiType,
List<UUID> genreIds
) {

public Artist toArtist() {
public Artist toArtist(String imageUrl) {
return Artist.builder()
.koreanName(koreanName)
.englishName(englishName)
.image(imageUrl)
.country(country)
.artistGender(ArtistGender.valueOf(artistGenderApiType.name()))
.artistType(ArtistType.valueOf(artistApiType.name()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public record ArtistDetailServiceResponse(
UUID id,
String koreanName,
String englishName,
String image,
String country,
ArtistGenderApiType artistGenderApiType,
ArtistApiType artistApiType,
Expand All @@ -21,6 +22,7 @@ public ArtistDetailServiceResponse(ArtistDetailResponse artistDetailResponse) {
artistDetailResponse.id(),
artistDetailResponse.koreanName(),
artistDetailResponse.englishName(),
artistDetailResponse.image(),
artistDetailResponse.country(),
ArtistGenderApiType.valueOf(artistDetailResponse.artistGender().name()),
ArtistApiType.valueOf(artistDetailResponse.artistType().name()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.artist.service.dto.response;

import java.util.UUID;
import org.example.dto.artist.response.ArtistKoreanNameResponse;

public record ArtistKoreanNameServiceResponse(
UUID id,
String koreanName
) {
public ArtistKoreanNameServiceResponse(ArtistKoreanNameResponse artistKoreanNameResponse) {
this(
artistKoreanNameResponse.id(),
artistKoreanNameResponse.koreanName()
);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.component;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

@Component
public interface FileUploadComponent {

String uploadFile(String directory, MultipartFile multipartFile);

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package com.example.genre.controller.dto.response;

import com.example.genre.service.dto.response.GenreNameServiceResponse;
import java.util.UUID;

public record GenreNameApiFormResponse(
UUID id,
String name
) {

public GenreNameApiFormResponse(GenreNameServiceResponse genreNameServiceResponse) {
this (
genreNameServiceResponse.id(),
genreNameServiceResponse.name()
);
}

}
Loading

0 comments on commit d1264e1

Please sign in to comment.