From ea611db970003af690f80a3f97cac970d6401ee3 Mon Sep 17 00:00:00 2001 From: jungmin Date: Mon, 15 Jul 2024 17:09:19 +0900 Subject: [PATCH] =?UTF-8?q?feat:=EC=9C=A0=EC=A0=80=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/build.gradle | 4 ++ .../edufocus/swagger/SwaggerConfig.java | 20 +++++++++ .../user/controller/UserController.java | 41 ++++++++++++++++++ .../edufocus/user/model/entity/User.java | 28 ++++++++++++ .../edufocus/user/model/entity/UserRole.java | 5 +++ .../user/model/exception/UserException.java | 8 ++++ .../user/model/repository/UserRepository.java | 8 ++++ .../user/model/service/UserService.java | 8 ++++ .../user/model/service/UserServiceImpl.java | 43 +++++++++++++++++++ .../src/main/resources/application.properties | 8 ++-- 10 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 backend/src/main/java/com/edufocus/edufocus/swagger/SwaggerConfig.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/user/model/entity/UserRole.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/user/model/exception/UserException.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/user/model/repository/UserRepository.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/user/model/service/UserService.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java diff --git a/backend/build.gradle b/backend/build.gradle index 6c0680f..ad4e920 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -23,6 +23,10 @@ dependencies { implementation 'mysql:mysql-connector-java:8.0.33' testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation group: 'io.livekit', name: 'livekit-server', version: '0.6.1' + implementation 'org.projectlombok:lombok' + annotationProcessor 'org.projectlombok:lombok' + implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0' + } diff --git a/backend/src/main/java/com/edufocus/edufocus/swagger/SwaggerConfig.java b/backend/src/main/java/com/edufocus/edufocus/swagger/SwaggerConfig.java new file mode 100644 index 0000000..7d71ef7 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/swagger/SwaggerConfig.java @@ -0,0 +1,20 @@ +package com.edufocus.edufocus.swagger; + +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class SwaggerConfig { + + @Bean + public OpenAPI openAPI() { + return new OpenAPI() + .info(new Info() + .title("에듀포커스 API") + .description("") + .version("1.0.0")); + } + } \ No newline at end of file diff --git a/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java b/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java new file mode 100644 index 0000000..19b306d --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java @@ -0,0 +1,41 @@ +package com.edufocus.edufocus.user.controller; + +import com.edufocus.edufocus.user.model.entity.User; +import com.edufocus.edufocus.user.model.service.UserService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/user") +@Slf4j +@RequiredArgsConstructor +public class UserController { + + private final UserService userService; + + @PostMapping("/join") + public ResponseEntity join(@RequestBody User user) throws Exception { + + System.out.println("!!!"); + userService.join(user); + return ResponseEntity.ok("User registered successfully"); + } + + + @PostMapping("/login") + public ResponseEntity login(@RequestBody User user) { + try { + User loggedInUser = userService.login(user); + return ResponseEntity.ok(loggedInUser); + + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java new file mode 100644 index 0000000..8914cd3 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java @@ -0,0 +1,28 @@ +package com.edufocus.edufocus.user.model.entity; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) // 자동 증가 설정 + private Long id; + + private String user_id; + private String email; + private String password; + @Enumerated(EnumType.STRING) // 혹은 EnumType.ORDINAL + private UserRole role; + + + +} diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/UserRole.java b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/UserRole.java new file mode 100644 index 0000000..48f8cc1 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/UserRole.java @@ -0,0 +1,5 @@ +package com.edufocus.edufocus.user.model.entity; + +public enum UserRole { + STUDENT,ADMIN +} diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/exception/UserException.java b/backend/src/main/java/com/edufocus/edufocus/user/model/exception/UserException.java new file mode 100644 index 0000000..5c73dc1 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/exception/UserException.java @@ -0,0 +1,8 @@ +package com.edufocus.edufocus.user.model.exception; + +public class UserException extends RuntimeException{ + + public UserException(String message){ + super(message); + } +} diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/repository/UserRepository.java b/backend/src/main/java/com/edufocus/edufocus/user/model/repository/UserRepository.java new file mode 100644 index 0000000..d05e98e --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/repository/UserRepository.java @@ -0,0 +1,8 @@ +package com.edufocus.edufocus.user.model.repository; + +import com.edufocus.edufocus.user.model.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { + +} diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserService.java b/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserService.java new file mode 100644 index 0000000..ba32a4f --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserService.java @@ -0,0 +1,8 @@ +package com.edufocus.edufocus.user.model.service; + +import com.edufocus.edufocus.user.model.entity.User; + +public interface UserService { + void join(User user) throws Exception; + User login(User user) throws Exception; +} diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java new file mode 100644 index 0000000..e2719bf --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java @@ -0,0 +1,43 @@ +package com.edufocus.edufocus.user.model.service; + + +import com.edufocus.edufocus.user.model.entity.User; +import com.edufocus.edufocus.user.model.exception.UserException; +import com.edufocus.edufocus.user.model.repository.UserRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.sql.SQLException; +import java.util.Optional; + +@Service +@Transactional +@RequiredArgsConstructor +public class UserServiceImpl implements UserService{ + + @Autowired + private final UserRepository userRepository; + + + public void join(User user) + { + userRepository.save(user); + } + + public User login(User user) throws SQLException + { + Optional findUser = userRepository.findById(user.getId()); + + if(findUser.isEmpty()) + { + throw new UserException("없는 유저"); + + } + return findUser.get(); + } + + + +} diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 172132c..8ec1189 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1,6 +1,6 @@ spring.application.name=edufocus +server.port=8080 -server.port=${SERVER_PORT:6080} server.ssl.enabled=false # LiveKit configuration @@ -8,10 +8,12 @@ livekit.api.key=${LIVEKIT_API_KEY:devkey} livekit.api.secret=${LIVEKIT_API_SECRET:secret} spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.datasource.url=jdbc:mysql://localhost:3306/edufocus?serverTimezone=Asia/Seoul +spring.datasource.url=jdbc:mysql://localhost:3306/edufocus?useSSL=false spring.datasource.username=root spring.datasource.password=root +spring.mvc.pathmatch.matching-strategy=ant_path_matcher spring.jpa.database=mysql -spring.jpa.hibernate.ddl-auto=update +spring.jpa.hibernate.ddl-auto=create spring.jpa.show-sql=true +