Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.gltkorea.icebang.domain.auth.controller;

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;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import com.gltkorea.icebang.common.dto.ApiResponse;
import com.gltkorea.icebang.domain.auth.dto.RegisterDto;
import com.gltkorea.icebang.domain.auth.service.AuthService;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -15,10 +14,12 @@
@RequestMapping("/v0/auth")
@RequiredArgsConstructor
public class AuthController {
private final AuthService authService;

@PostMapping("/register")
public ApiResponse<String> register(@Valid @RequestBody RegisterDto registerDto) {

throw new RuntimeException("Not implemented");
@ResponseStatus(HttpStatus.CREATED)
public ApiResponse<Void> register(@Valid @RequestBody RegisterDto registerDto) {
authService.registerUser(registerDto);
return ApiResponse.success(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
@AllArgsConstructor
@NoArgsConstructor
public class RegisterDto {
@Null private BigInteger id;

@NotBlank(message = "사용자명은 필수입니다")
private String userName;
private String name;

@NotBlank(message = "이메일은 필수입니다")
@Email(message = "올바른 이메일 형식이 아닙니다")
private String email;

@Null private BigInteger userOrgId;

@NotNull(message = "조직 선택은 필수입니다")
private BigInteger orgId;

Expand All @@ -38,4 +41,6 @@ public class RegisterDto {
private Set<BigInteger> roleIds;

@Null private String password;

@Null private String status;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ public class AuthService {
private final EmailService emailService;

public void registerUser(RegisterDto registerDto) {
if (authMapper.existsByEmail(registerDto.getEmail())) {
throw new IllegalArgumentException("이미 가입된 이메일입니다.");
}
String randomPassword = passwordGenerator.generate();
String hashedPassword = passwordEncoder.encode(randomPassword);

registerDto.setPassword(hashedPassword);
registerDto.setStatus("PENDING");

// @TODO:: Auth mapper 호출하여 insert
authMapper.insertUser(registerDto);

// 2. user_organizations insert → userOrgId 반환
authMapper.insertUserOrganization(registerDto);

// 3. user_roles insert (foreach)
if (registerDto.getRoleIds() != null && !registerDto.getRoleIds().isEmpty()) {
authMapper.insertUserRoles(registerDto);
}

EmailRequest emailRequest =
EmailRequest.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class UserService {
public void registerUser(RegisterDto registerDto) {
Users user =
Users.builder()
.name(registerDto.getUserName())
.name(registerDto.getName())
.email(registerDto.getEmail())
.password(registerDto.getPassword())
.status("PENDING")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@

@Mapper
public interface AuthMapper {
void registerUser(RegisterDto registerDto);
boolean existsByEmail(String email);

int insertUser(RegisterDto dto); // users insert

int insertUserOrganization(RegisterDto dto); // user_organizations insert

int insertUserRoles(RegisterDto dto); // user_roles insert (foreach)
}
36 changes: 26 additions & 10 deletions apps/user-service/src/main/resources/mybatis/mapper/AuthMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,35 @@
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.gltkorea.icebang.mapper.AuthMapper">
<insert id="registerUser" parameterType="com.gltkorea.icebang.domain.auth.dto.RegisterDto">
<selectKey keyProperty="id" resultType="java.math.BigInteger" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>

<!-- 이메일 중복 여부 확인 -->
<select id="existsByEmail" parameterType="string" resultType="boolean">
SELECT EXISTS(
SELECT 1
FROM users
WHERE email = #{email}
)
</select>

<!-- 1. users INSERT & PK 반환 -->
<insert id="insertUser" parameterType="com.gltkorea.icebang.domain.auth.dto.RegisterDto" useGeneratedKeys="true" keyProperty="id">
INSERT INTO users (name, email, password)
VALUES (#{name}, #{email}, #{password})
VALUES (#{name}, #{email}, #{password});
</insert>

<!-- 2. user_organizations INSERT -->
<insert id="insertUserOrganization" parameterType="com.gltkorea.icebang.domain.auth.dto.RegisterDto" useGeneratedKeys="true" keyProperty="userOrgId">
INSERT INTO user_organizations (user_id, organization_id, department_id, position_id, status)
VALUES (#{id}, #{organizationId}, #{departmentId}, #{positionId}, #{status})
VALUES (#{id}, #{orgId}, #{deptId}, #{positionId}, #{status});
</insert>

<!-- 3. user_roles INSERT (foreach) -->
<insert id="insertUserRoles" parameterType="com.gltkorea.icebang.domain.auth.dto.RegisterDto">
INSERT INTO user_roles (user_organization_id, role_id)
SELECT uo.id, #{roleId}
FROM user_organizations uo
WHERE uo.user_id = #{id}
VALUES
<foreach collection="roleIds" item="roleId" separator=",">
(#{userOrgId}, #{roleId})
</foreach>
</insert>
</mapper>

</mapper>
Loading