본문 바로가기

카테고리 없음

24.01.18 인가3

import java.time.Duration
이걸 넣고
fun generateAccessToken(subject: String, role: String): String {
    return generateToken(subject, role, Duration.ofHours(accessTokenExpirationHour))
}
이러자 해결

 

package com.teamsparta.todo.domain.dto

data class TodoDto(
    val id: Long?,
    val title: String,
    val content: String,
    val authorName: String
)
companion object {
    fun from(todo: com.teamsparta.todo.domain.model.Todo): TodoDto {
        return TodoDto(
            id = todo.id,
            title = todo.title,
            content = todo.content,
            authorName = todo.authorName
        )
    }
}

 

 

이랬는데 컴패니언 오브젝트에 빨간줄이 생기자

package com.teamsparta.todo.domain.dto

data class TodoDto(
    val id: Long?,
    val title: String,
    val content: String,
    val authorName: String
) {
    companion object {
        fun from(todo: com.teamsparta.todo.domain.model.Todo): TodoDto {
            return TodoDto(
                id = todo.id,
                title = todo.title,
                content = todo.content,
                authorName = todo.authorName
            )
        }
    }
}

 

 

이렇게 {}사이에 넣어서 해결하고

강의를 보면서 배운 로그인 관련 메서ㅡ나 이것저것들을 다 집어넣었는데도 계속 오류나 뜨고, 빨간색도 아닌 빨간줄들이 생기질 않나, 빨간색이면 뭐 빼먹은거니 채우면 되는데 빨간줄은 빼먹지 않았는데도 그러는거 같은데 그래서 더 악질인거 같다.

 

대부분은 alt enter이면 되는데 안되는것들이 섞여있다!!

 

package com.teamsparta.todo.domain.service

import com.teamsparta.todo.domain.dto.MemberCreateDto
import com.teamsparta.todo.domain.dto.MemberDto
import com.teamsparta.todo.domain.dto.MemberUpdateDto
import com.teamsparta.todo.domain.login.LoginResponse
import com.teamsparta.todo.domain.model.Member
import com.teamsparta.todo.domain.repository.MemberRepository
import com.teamsparta.todo.infra.security.jwt.JwtPlugin
import jakarta.transaction.Transactional
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.stereotype.Service

@Service
class MemberServiceImpl(
    private val memberRepository: MemberRepository,
    private val passwordEncoder: BCryptPasswordEncoder,
    private val jwtPlugin: JwtPlugin
) : MemberService {

    @Transactional
    override fun signUp(memberCreateDto: MemberCreateDto): MemberDto {
        if (memberRepository.existsByEmail(memberCreateDto.email)) {
            throw IllegalStateException("Email is already in use")
        }

        val encryptedPassword = passwordEncoder.encode(memberCreateDto.password)
        val member = Member(memberCreateDto.email, encryptedPassword)
        val savedMember = memberRepository.save(member)
        return MemberDto.from(savedMember)
    }

    @Transactional
    override fun login(memberDto: MemberDto): LoginResponse {
        val member = memberRepository.findByEmail(memberDto.email)
            ?: throw IllegalArgumentException("No member found with the given email")

        if (!passwordEncoder.matches(memberDto.password, member.password)) {
            throw IllegalArgumentException("Invalid password")
        }

        val token = jwtPlugin.generateToken(member)
        return LoginResponse(token)
    }

    @Transactional
    override fun updateMemberProfile(id: Long, memberUpdateDto: MemberUpdateDto): MemberDto {
        val member = memberRepository.findById(id).orElseThrow { IllegalArgumentException("Member not found") }

        if(memberUpdateDto.password != null) {
            member.password = passwordEncoder.encode(memberUpdateDto.password)
        }

        member.username = memberUpdateDto.username ?: member.username
        member.email = memberUpdateDto.email ?: member.email

        val updatedMember = memberRepository.save(member)
        return MemberDto.from(updatedMember)
    }

    @Transactional
    override fun update(id: Long, memberUpdateDto: MemberUpdateDto): MemberDto {
        val member = memberRepository.findById(id).orElseThrow { IllegalArgumentException("Member not found") }
        member.update(memberUpdateDto.username, memberUpdateDto.password, memberUpdateDto.email)
        val updatedMember = memberRepository.save(member)
        return MemberDto.from(updatedMember)
    }

    @Transactional
    override fun delete(id: Long) {
        val member = memberRepository.findById(id)
            .orElseThrow { IllegalArgumentException("No member found with the given id") }

        memberRepository.delete(member)
    }
    @Transactional
    override fun findById(id: Long): MemberDto {
        val member = memberRepository.findById(id).orElseThrow { IllegalArgumentException("Member not found") }
        return MemberDto.from(member)
    }
}

 

여기에서 passwordEncoder가 빨간줄이였는데, 검색해봐도 답이안나와서 알트엔터로 본 검사옵션을 보니까

  • 오토와이어링할 수 없습니다. 'BCryptPasswordEncoder' 타입의 bean을 찾을 수 없습니다.

라고 떴다. 분명 한거같은데??

val member = Member(memberCreateDto.email, encryptedPassword)

 

 

여기서도 encryptedPassword가 빨간줄이였는데 이건 다행히

val member = Member(memberCreateDto.username, memberCreateDto.email, encryptedPassword)

 

이렇게 해결했다. 중간에 이메일을 거추장스러울거 같아서 네임으로 했는데 이건 빼먹었던 모양이다.

 

그 외에도 고칠게 많다. 힘들다..