diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java index 8721db0..7defc85 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java @@ -35,11 +35,11 @@ public class Spot extends BaseEntity { @Column(name = "img_url", nullable = false) private String imgUrl; - @Column(name = "like_num", nullable = false) - private Long likeNum; + @Column(name = "like_num") + private Long likeNum = 0L; - @Column(name = "visit_num", nullable = false) - private Long visitNum; + @Column(name = "visit_num") + private Long visitNum =0L; @Column(name = "title", nullable = false) private String title; diff --git a/src/main/java/com/umc/hackaton/snapspot/user/repository/UserRepository.java b/src/main/java/com/umc/hackaton/snapspot/user/repository/UserRepository.java index 0b3cff5..a6b441a 100644 --- a/src/main/java/com/umc/hackaton/snapspot/user/repository/UserRepository.java +++ b/src/main/java/com/umc/hackaton/snapspot/user/repository/UserRepository.java @@ -6,7 +6,7 @@ import java.util.Optional; public interface UserRepository extends JpaRepository { - Optional findByUserId(String userId); + Optional findByUsername(String username); User findUserById(Long id); } diff --git a/src/main/java/com/umc/hackaton/snapspot/user/service/UserService.java b/src/main/java/com/umc/hackaton/snapspot/user/service/UserService.java index 6345606..96a5080 100644 --- a/src/main/java/com/umc/hackaton/snapspot/user/service/UserService.java +++ b/src/main/java/com/umc/hackaton/snapspot/user/service/UserService.java @@ -1,16 +1,22 @@ package com.umc.hackaton.snapspot.user.service; -import com.umc.hackaton.snapspot.user.dto.UserRequestDto; +import com.umc.hackaton.snapspot.user.dto.request.ImageUploadDto; +import com.umc.hackaton.snapspot.user.dto.request.LoginDto; +import com.umc.hackaton.snapspot.user.dto.request.UserRequestDto; +import com.umc.hackaton.snapspot.user.dto.response.UserResponseDto; import com.umc.hackaton.snapspot.user.entity.User; import com.umc.hackaton.snapspot.user.repository.UserRepository; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import lombok.extern.slf4j.Slf4j; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; +import java.util.Optional; + @Service @RequiredArgsConstructor +@Slf4j public class UserService { private final UserRepository userRepository; @@ -18,13 +24,35 @@ public class UserService { @Transactional public void signUp(UserRequestDto dto) { - -// Optional existingUser = userRepository.findByUserId(dto.getUserId()); -// if (existingUser.isPresent()) { -// User user = existingUser.get(); -// } else { User user = dto.toEntity(encoder); userRepository.save(user); } + + @Transactional + public UserResponseDto login(LoginDto dto) { + Optional loginuser = userRepository.findByUsername(dto.getUsername()); + if (loginuser.isEmpty()) { + throw new IllegalArgumentException("존재하지 않는 사용자입니다."); + } + + if (!encoder.matches(dto.getPassword(), loginuser.get().getPassword())) { + throw new IllegalArgumentException("비밀번호가 일치하지 않습니다."); + } + + return UserResponseDto.builder() + .id(loginuser.get().getId()) + .username(loginuser.get().getUsername()) + .build(); + } + + @Transactional + public void imageUploads(ImageUploadDto dto) { + Optional user = userRepository.findById(dto.getId()); + if (user.isEmpty()) { + throw new IllegalArgumentException("존재하지 않는 사용자입니다."); + } + user.get().updateProfileImg(dto.getImage()); + + } }