TIL 23.12.07

한승준·2023년 12월 7일

TIL

목록 보기
33/41

3-Layered Architecture

controller

   // 회원가입
   signUp = async (req, res, next) => {
      try {
         const { name, email, password, passwordRe, description } = req.body;
         const createdUser = await this.usersService.createUser(name, email, password, passwordRe, description);
         return res.status(201).json({ message: '성공적으로 회원가입', createdUser: createdUser });
      } catch (error) {
         next(error);
      }
   };

service

  createUser = async (name, email, password, passwordRe, description) => {
      try {
         if (!name || !email || !password || !passwordRe) {
            throw new Error('값을 모두 입력해라');
         }
         if (password !== passwordRe) {
            throw new Error('입력한 비밀번호가 다름');
         }
         const isExistUser = await this.usersRepository.findUserByEmail(email);
         if (isExistUser) {
            throw new Error('이미 존재하는 email 입니다');
         }
         const sortPassword = await bcrypt.hash(password, 11);
         const createdUser = await this.usersRepository.createUser(name, email, sortPassword, description);

         return createdUser;
      } catch (error) {
         throw error;
      }
   };

repository

   createUser = async (name, email, sortPassword, description) => {
      const createdUser = await prisma.Users.create({
         data: { name, email, password: sortPassword, description },
      });
      return createdUser;
   };
profile
한승준

0개의 댓글