Skip to content

Commit

Permalink
Merge pull request #718 from woowacourse-teams/dev/be
Browse files Browse the repository at this point in the history
[BE] 코드잽 프로덕션 v1.1.3 배포
  • Loading branch information
jminkkk authored Oct 8, 2024
2 parents 7d34058 + e2115dd commit a168509
Show file tree
Hide file tree
Showing 62 changed files with 1,732 additions and 780 deletions.
4 changes: 4 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ out/

### YAML ###
application-db.yml

### compose ###
docker/app/*.jar
docker/.env
24 changes: 24 additions & 0 deletions backend/docker/app/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
spring:
config:
activate:
on-profile: local
datasource:
url: jdbc:mysql://mysql:3306/${MYSQL_DATABASE}?serverTimezone=Asia/Seoul
username: root
password: ${MYSQL_ROOT_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver
flyway:
enabled: false
jpa:
hibernate:
ddl-auto: create-drop
properties:
hibernate:
format_sql: true
show_sql: true
output:
ansi:
enabled: always

#cors:
# allowed-origins: http://localhost:3000
35 changes: 35 additions & 0 deletions backend/docker/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: code-zap

services:
mysql:
image: mysql:8.4.2
expose:
- ${MYSQL_PORT}
ports:
- ${HOST_PORT}:${MYSQL_PORT}
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
TZ: Asia/Seoul
volumes:
- code-zap:/var/lib/mysql

spring:
image: amazoncorretto:17
ports:
- "8080:8080"
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
TZ: Asia/Seoul
volumes:
- ./app:/app
entrypoint: [
"java", "-jar",
"-Dspring.config.location=/app/application.yml",
"-Dspring.profiles.active=local",
"/app/zap.jar"
]

volumes:
code-zap:
20 changes: 20 additions & 0 deletions backend/docker/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

abs_path=$(readlink -f "$0");
abs_dir=$(dirname "$abs_path");

backend_path=$(realpath "$abs_path/../..")

cd "$backend_path" || exit

./gradlew clean
./gradlew bootJar

jar_path="$backend_path/build/libs"
jar_filename=$(ls -tr "$jar_path"/*.jar | grep ".*\.jar")

mkdir "$abs_dir/app"
mv "$jar_filename" "$abs_dir/app/zap.jar"

cd "$abs_dir" || exit
docker compose up -d
15 changes: 8 additions & 7 deletions backend/src/main/java/codezap/category/domain/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
Expand All @@ -24,12 +26,11 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Table(
uniqueConstraints = {
@UniqueConstraint(
name = "name_with_member",
columnNames = {"member_id", "name"}
)
}
uniqueConstraints = @UniqueConstraint(
name = "name_with_member",
columnNames = {"member_id", "name"}
),
indexes = @Index(name = "idx_member_id", columnList = "member_id")
)
@Getter
@EqualsAndHashCode(of = "id", callSuper = false)
Expand All @@ -41,7 +42,7 @@ public class Category extends BaseTimeEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(optional = false)
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private Member member;

@Column(nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public record CreateCategoryRequest(
@Schema(description = "카테고리 이름", example = "Spring")
@NotBlank(message = "카테고리 이름이 null 입니다.", groups = NotNullGroup.class)
@Size(max = 255, message = "카테고리 이름은 최대 255자까지 입력 가능합니다.", groups = SizeCheckGroup.class)
@Size(max = 15, message = "카테고리 이름은 최대 15자까지 입력 가능합니다.", groups = SizeCheckGroup.class)
String name
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private String getExampleJsonString(ProblemDetail problemDetail) {
*/
private void hideAuthInfoParameter(Operation operation) {
if (operation.getParameters() != null) {
operation.getParameters().removeIf(parameter -> parameter.getName().equals("memberDto"));
operation.getParameters().removeIf(parameter -> parameter.getName().equals("member"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ public OpenAPI openAPI() {
List.of(
new Server()
.url("https://api.code-zap.com")
.description("클라우드에 배포되어 있는 테스트 서버입니다."),
.description("운영 서버"),
new Server()
.url("https://dev.code-zap.com")
.description("개발 서버"),
new Server()
.url("http://localhost:8080")
.description("BE 팀에서 사용할 로컬 환경 테스트를 위한 서버입니다.")
.description("로컬 서버")
)
)
.addSecurityItem(new SecurityRequirement().addList("쿠키 인증 토큰"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ByteLengthValidator.class)
@Constraint(validatedBy = {ByteLengthValidator.class, GroupedByteLengthValidator.class})
public @interface ByteLength {

String message();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package codezap.global.validation;

import java.nio.charset.StandardCharsets;
import java.util.List;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class GroupedByteLengthValidator implements ConstraintValidator<ByteLength, List<String>> {

private int max;
private int min;

@Override
public void initialize(ByteLength constraintAnnotation) {
max = constraintAnnotation.max();
min = constraintAnnotation.min();
}

@Override
public boolean isValid(List<String> strings, ConstraintValidatorContext constraintValidatorContext) {
return strings.stream()
.allMatch(this::isValid);
}

private boolean isValid(String target) {
int byteLength = target.getBytes(StandardCharsets.UTF_8).length;
return min <= byteLength && byteLength <= max;
}
}
7 changes: 6 additions & 1 deletion backend/src/main/java/codezap/likes/domain/Likes.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codezap.likes.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
Expand Down Expand Up @@ -29,6 +30,10 @@ public class Likes extends BaseTimeEntity {
@ManyToOne(optional = false)
private Template template;

@ManyToOne(optional = false)
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private Member member;

public Likes(Template template, Member member) {
this(null, template, member);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package codezap.likes.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import codezap.likes.domain.Likes;

public interface LikesJpaRepository extends LikesRepository, JpaRepository<Likes, Long> {

@Modifying(clearAutomatically = true)
@Query("DELETE FROM Likes l WHERE l.template.id in :templateIds")
void deleteByTemplateIds(List<Long> templateIds);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package codezap.likes.repository;

import java.util.List;

import codezap.likes.domain.Likes;
import codezap.member.domain.Member;
import codezap.template.domain.Template;
Expand All @@ -12,4 +14,6 @@ public interface LikesRepository {
long countByTemplate(Template template);

void deleteByMemberAndTemplate(Member member, Template template);

void deleteByTemplateIds(List<Long> templateIds);
}
7 changes: 7 additions & 0 deletions backend/src/main/java/codezap/likes/service/LikesService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package codezap.likes.service;

import java.util.List;

import jakarta.transaction.Transactional;

import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -37,4 +39,9 @@ public void cancelLike(Member member, long templateId) {
Template template = templateRepository.fetchById(templateId);
likesRepository.deleteByMemberAndTemplate(member, template);
}

@Transactional
public void deleteAllByTemplateIds(List<Long> templateIds) {
likesRepository.deleteByTemplateIds(templateIds);
}
}
13 changes: 0 additions & 13 deletions backend/src/main/java/codezap/member/dto/MemberDto.java

This file was deleted.

3 changes: 3 additions & 0 deletions backend/src/main/java/codezap/tag/domain/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.Table;

import codezap.global.auditing.BaseTimeEntity;
import lombok.AccessLevel;
Expand All @@ -18,6 +20,7 @@
@AllArgsConstructor
@Getter
@EqualsAndHashCode(of = "id", callSuper = false)
@Table(indexes = @Index(name = "idx_tag_name", columnList = "name"))
public class Tag extends BaseTimeEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface TagRepository {

Optional<Tag> findByName(String name);

List<String> findNameByNamesIn(List<String> names);
List<Tag> findByNameIn(List<String> names);

Tag save(Tag tag);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,53 @@
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import codezap.tag.domain.Tag;
import codezap.template.domain.Template;
import codezap.template.domain.TemplateTag;

@SuppressWarnings("unused")
public interface TemplateTagJpaRepository extends TemplateTagRepository, JpaRepository<TemplateTag, Long> {

List<TemplateTag> findAllByTemplate(Template template);

@Query("""
SELECT DISTINCT tt.id.tagId
FROM TemplateTag tt
WHERE tt.id.templateId IN :templateIds
SELECT t
FROM Tag t
JOIN TemplateTag tt ON t.id = tt.id.tagId
WHERE tt.template = :template
""")
List<Long> findDistinctByTemplateIn(List<Long> templateIds);
List<Tag> findAllTagsByTemplate(Template template);

@Query("""
SELECT tt, t
FROM TemplateTag tt
JOIN FETCH tt.tag t
WHERE tt.id.templateId = :templateId
""")
List<TemplateTag> findAllByTemplateId(Long templateId);

@Query("""
SELECT tt, t
FROM TemplateTag tt
JOIN FETCH tt.tag t
WHERE tt.id.templateId in :templateIds
""")
List<TemplateTag> findAllByTemplateIdsIn(List<Long> templateIds);

@Query("""
SELECT DISTINCT t
FROM Tag t
WHERE t.id IN (
SELECT DISTINCT tt.id.tagId
FROM TemplateTag tt
WHERE tt.id.templateId IN
(SELECT te.id FROM Template te WHERE te.member.id = :memberId)
)
""")
List<Tag> findAllTagDistinctByMemberId(Long memberId);

void deleteAllByTemplateId(Long id);
@Modifying(clearAutomatically = true)
@Query("DELETE FROM TemplateTag t WHERE t.template.id in :templateIds")
void deleteByTemplateIds(List<Long> templateIds);
}
Loading

0 comments on commit a168509

Please sign in to comment.