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

Feature/1 #12

Merged
merged 11 commits into from
Jul 24, 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
9 changes: 9 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ dependencies {
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.11.5")
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.11.5")

//kubernetes client lib
implementation("io.fabric8:kubernetes-client:6.12.0")

//Freemarker
implementation("org.springframework.boot:spring-boot-starter-freemarker")

//JGit
implementation("org.eclipse.jgit:org.eclipse.jgit:6.7.0.202309050840-r")

runtimeOnly("com.h2database:h2")
compileOnly("org.projectlombok:lombok")
annotationProcessor("org.projectlombok:lombok")
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/com/project/workaholic/account/api/AccountApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@
import com.project.workaholic.account.service.AccountService;
import com.project.workaholic.config.interceptor.JsonWebToken;
import com.project.workaholic.config.interceptor.JsonWebTokenProvider;
import com.project.workaholic.response.model.ApiResponse;
import com.project.workaholic.response.model.enumeration.StatusCode;
import com.project.workaholic.config.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Tag(name = "Account API", description = "Workaholic 계정 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/account")
public class AccountApi {
private final AccountService accountService;
private final JsonWebTokenProvider jsonWebTokenProvider;

public AccountApi(AccountService accountService, JsonWebTokenProvider jsonWebTokenProvider) {
this.accountService = accountService;
this.jsonWebTokenProvider = jsonWebTokenProvider;
}

private Account toEntity(AccountSignIdDto dto) {
return Account.builder()
.id(dto.getId())
Expand Down Expand Up @@ -70,34 +72,34 @@ public ResponseEntity<ApiResponse<AccountSignIdDto>> login(
refreshTokenCookie.setDomain("example.com"); // Set the domain if needed
response.addCookie(refreshTokenCookie);

return ApiResponse.success(StatusCode.SUCCESS_LOGIN, toSignIdDto(accountToVerify));
return ApiResponse.success(toSignIdDto(accountToVerify));
}

@Operation(summary = "계정 로그아웃", description = "Workaholic Service 로그아웃")
@PostMapping("/logout")
public ResponseEntity<ApiResponse<StatusCode>> logout() {
return ApiResponse.success(StatusCode.SUCCESS_LOGOUT);
public ResponseEntity<Void> logout() {
return ApiResponse.success();
}

@Operation(summary = "계정 회원가입", description = "Workaholic Service 회원가입")
@PostMapping("/signup")
public ResponseEntity<ApiResponse<StatusCode>> signup(
public ResponseEntity<Void> signup(
final @Parameter(name = "회원가입 폼", description = "회원가입을 위한 계정 정보")
@Valid @RequestBody AccountRegisterDto accountRegisterDto) {
Account accountToRegister = toEntity(accountRegisterDto);
accountService.registerAccount(accountToRegister);
return ApiResponse.success(StatusCode.SUCCESS_SIGNUP);
return ApiResponse.success();
}

@Operation(summary = "계정 삭제", description = "Workaholic Service 탈퇴")
@PostMapping("/delete")
public ResponseEntity<ApiResponse<StatusCode>> deleteAccount() {
return ApiResponse.success(StatusCode.SUCCESS_DELETE_ACCOUNT);
public ResponseEntity<Void> deleteAccount() {
return ApiResponse.success();
}

@Operation(summary = "계정 수정", description = "Workaholic Service 계정 정보 변경")
@PutMapping("")
public ResponseEntity<ApiResponse<StatusCode>> updateAccount() {
return ApiResponse.success(StatusCode.SUCCESS_UPDATE_ACCOUNT);
public ResponseEntity<Void> updateAccount() {
return ApiResponse.success();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,39 @@
import com.project.workaholic.account.model.entity.Account;
import com.project.workaholic.account.repository.AccountRepository;
import com.project.workaholic.config.encoder.PasswordEncoder;
import com.project.workaholic.config.exception.CustomException;
import com.project.workaholic.response.model.enumeration.StatusCode;
import com.project.workaholic.config.exception.type.DuplicateAccountException;
import com.project.workaholic.config.exception.type.InvalidAccountException;
import com.project.workaholic.config.exception.type.NotFoundAccountException;
import com.project.workaholic.deploy.service.DeployService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class AccountService {
private final PasswordEncoder passwordEncoder;
private final AccountRepository accountRepository;
private final DeployService deployService;

@Transactional
public void registerAccount(Account account) {
if( accountRepository.existsById(account.getId()))
throw new CustomException(StatusCode.EXISTS_ACCOUNT_ID);
throw new DuplicateAccountException();
account.setPassword(passwordEncoder.encrypt(account.getId(), account.getPassword()));
accountRepository.save(account);
account = accountRepository.save(account);
deployService.createNamespaceByAccountId(account.getId());
}

public Account verifyAccount(Account account) {
return accountRepository.findById(account.getId())
.filter( target -> target.getPassword().equals(passwordEncoder.encrypt(account.getId(), account.getPassword())))
.orElseThrow(() -> new CustomException(StatusCode.INVALID_ACCOUNT));
.orElseThrow(InvalidAccountException::new);
}

public Account getAccountById(String id) {
return accountRepository.findById(id)
.orElseThrow(() -> new CustomException(StatusCode.INVALID_ACCOUNT));
.orElseThrow(NotFoundAccountException::new);
}

public boolean checkExistAccountById(String accountId) {
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/project/workaholic/config/ApiResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.project.workaholic.config;

import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.time.LocalDateTime;

@Getter
public class ApiResponse<T> {
private final LocalDateTime localDateTime = LocalDateTime.now();
private final T data;

public ApiResponse(T data) {
this.data = data;
}

public static ResponseEntity<Void> success() {
return ResponseEntity
.status(HttpStatus.OK)
.build();
}

public static <T> ResponseEntity<ApiResponse<T>> success(T data) {
return ResponseEntity
.status(HttpStatus.OK)
.body(new ApiResponse<T>(data));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.project.workaholic.config.converter;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Converter
public class ListToStringConverter implements AttributeConverter<List<String>, String> {
private static final String DELIMITER = ",";
@Override
public String convertToDatabaseColumn(List<String> attribute) {
if( attribute == null || attribute.isEmpty() )
return null;

return String.join(DELIMITER, attribute);
}

@Override
public List<String> convertToEntityAttribute(String dbData) {
if( dbData == null || dbData.isEmpty() )
return new ArrayList<>();

return new ArrayList<>(Arrays.asList(dbData.split(DELIMITER)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.project.workaholic.config.converter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.util.Map;

@Converter
public class MapToStringConverter implements AttributeConverter<Map<String, String>, String> {
private final ObjectMapper objectMapper = new ObjectMapper();

@Override
public String convertToDatabaseColumn(Map<String, String> attribute) {
if( attribute == null || attribute.isEmpty() )
return null;
try {
return objectMapper.writeValueAsString(attribute);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Error converting map to JSON", e);
}
}

@Override
public Map<String, String> convertToEntityAttribute(String entityData) {
if ( entityData == null )
return Map.of();
try {
return objectMapper.readValue(entityData, new TypeReference<>() {});
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Error converting map to JSON", e);
}
}
}

This file was deleted.

Loading
Loading