Skip to content

Commit

Permalink
Merge pull request #17 from UMC-HACKATHON-SnapSpot/feat/#12
Browse files Browse the repository at this point in the history
feat: 로그인 구현
  • Loading branch information
wonjun0120 authored Jul 4, 2024
2 parents 3be2123 + 13afd54 commit eaa095b
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.umc.hackaton.snapspot.user.controller;

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.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -33,11 +36,22 @@ public ResponseEntity<?> signUp(@RequestBody UserRequestDto dto) {
@PostMapping("/login")
public ResponseEntity<?> login(@RequestBody LoginDto dto) {
try {
userService.login(dto);
return ResponseEntity.ok().body("로그인 성공.");
UserResponseDto responseDto = userService.login(dto);
return ResponseEntity.ok( responseDto);
} catch (Exception e) {
log.info("로그인에 실패하였습니다.", e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("로그인에 실패하였습니다.");
}
}

@PostMapping("/image-uploads")
public ResponseEntity<?> imageUploads(@RequestBody ImageUploadDto dto) {
try {
userService.imageUploads(dto);
return ResponseEntity.ok().body("이미지 업로드 성공.");
} catch (Exception e) {
log.info("이미지 업로드에 실패하였습니다.", e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("이미지 업로드에 실패하였습니다.");
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.umc.hackaton.snapspot.user.dto.request;

import lombok.Getter;

@Getter
public class ImageUploadDto {
private Long id;
private String image;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.umc.hackaton.snapspot.user.dto;
package com.umc.hackaton.snapspot.user.dto.request;


import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.umc.hackaton.snapspot.user.dto;
package com.umc.hackaton.snapspot.user.dto.request;

import com.umc.hackaton.snapspot.user.entity.User;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.umc.hackaton.snapspot.user.dto.response;

import com.umc.hackaton.snapspot.user.entity.User;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class UserResponseDto {
@NotNull
private Long id;
private String username;

public static UserResponseDto fromEntity(User user) {
return UserResponseDto.builder()
.id(user.getId())
.username(user.getUsername())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;

@Entity
@Getter
Expand Down Expand Up @@ -38,4 +37,8 @@ public void prePersist() {
this.profileImg = "default";
}
}

public void updateProfileImg(String profileImg) {
this.profileImg = profileImg;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.umc.hackaton.snapspot.user.service;

import com.umc.hackaton.snapspot.user.dto.LoginDto;
import com.umc.hackaton.snapspot.user.dto.UserRequestDto;
import com.umc.hackaton.snapspot.user.dto.UserResponseDto;
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;
Expand All @@ -11,7 +12,6 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.swing.text.html.Option;
import java.util.Optional;

@Service
Expand Down Expand Up @@ -42,9 +42,16 @@ public UserResponseDto login(LoginDto dto) {
return UserResponseDto.builder()
.id(loginuser.get().getId())
.username(loginuser.get().getUsername())
.nickname(loginuser.get().getNickname())
.profileImg(loginuser.get().getProfileImg())
.build();
}

@Transactional
public void imageUploads(ImageUploadDto dto) {
Optional<User> user = userRepository.findById(dto.getId());
if (user.isEmpty()) {
throw new IllegalArgumentException("존재하지 않는 사용자입니다.");
}
user.get().updateProfileImg(dto.getImage());

}
}
Expand Down

0 comments on commit eaa095b

Please sign in to comment.