From a12ac0cfb34d3ab02565a5d422fbd4d93bcded7e Mon Sep 17 00:00:00 2001 From: do-dop Date: Thu, 4 Jul 2024 23:35:12 +0900 Subject: [PATCH 1/4] feat: merge dev --- .../spot/controller/SpotController.java | 31 ++++++++++++ .../hackaton/snapspot/spot/dto/SpotDto.java | 31 ++++++++++++ .../snapspot/spot/dto/SpotRequestDto.java | 16 ++++++ .../snapspot/spot/dto/SpotResponseDto.java | 4 ++ .../hackaton/snapspot/spot/entity/Spot.java | 49 +++++++++++++++++++ .../spot/repository/SpotRepository.java | 9 ++++ .../snapspot/spot/service/SpotService.java | 36 ++++++++++++++ .../user/repository/UserRepository.java | 3 ++ 8 files changed, 179 insertions(+) create mode 100644 src/main/java/com/umc/hackaton/snapspot/spot/controller/SpotController.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotDto.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotRequestDto.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotResponseDto.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/controller/SpotController.java b/src/main/java/com/umc/hackaton/snapspot/spot/controller/SpotController.java new file mode 100644 index 0000000..b0f394f --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/spot/controller/SpotController.java @@ -0,0 +1,31 @@ +package com.umc.hackaton.snapspot.spot.controller; + +import com.umc.hackaton.snapspot.spot.dto.SpotRequestDto; +import com.umc.hackaton.snapspot.spot.service.SpotService; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +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; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/v1/spots") +public class SpotController { + + + private final SpotService spotService; + + @PostMapping + public ResponseEntity upload(@RequestBody SpotRequestDto dto){ + try { + spotService.upload(dto); + return ResponseEntity.ok().body("스팟 업로드 성공."); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 업로드에 실패하였습니다."); + } + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotDto.java b/src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotDto.java new file mode 100644 index 0000000..5a05f77 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotDto.java @@ -0,0 +1,31 @@ +package com.umc.hackaton.snapspot.spot.dto; + +import com.umc.hackaton.snapspot.user.entity.User; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class SpotDto { + + private User user; + private Double latitude; + private Double longitude; + private String title; + private String description; + private String imgUrl; + + @Builder + public Spot toEntity() { + return Spot.builder() + .user(user) + .latitude(latitude) + .longitude(longitude) + .title(title) + .description(description) + .imgUrl(imgUrl) + .build(); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotRequestDto.java b/src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotRequestDto.java new file mode 100644 index 0000000..8ba1b8c --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotRequestDto.java @@ -0,0 +1,16 @@ +package com.umc.hackaton.snapspot.spot.dto; + +import com.umc.hackaton.snapspot.spot.entity.Spot; +import lombok.Builder; +import lombok.Getter; + +@Getter +public class SpotRequestDto { + private Long userId; + private Double latitude; + private Double longitude; + private String title; + private String description; + private String imgUrl; + +} diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotResponseDto.java b/src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotResponseDto.java new file mode 100644 index 0000000..61f1665 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotResponseDto.java @@ -0,0 +1,4 @@ +package com.umc.hackaton.snapspot.spot.dto; + +public class SpotResponseDto { +} 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 new file mode 100644 index 0000000..8721db0 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java @@ -0,0 +1,49 @@ +package com.umc.hackaton.snapspot.spot.entity; + +import com.umc.hackaton.snapspot.config.entity.BaseEntity; +import com.umc.hackaton.snapspot.user.entity.User; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +@Entity +@Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor +@Table(name = "spot") +public class Spot extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id", nullable = false) + private User user; + + @Column(name = "latitude", nullable = false) + private Double latitude; + + @Column(name = "longitude", nullable = false) + private Double longitude; + + @Column(name = "img_url", nullable = false) + private String imgUrl; + + @Column(name = "like_num", nullable = false) + private Long likeNum; + + @Column(name = "visit_num", nullable = false) + private Long visitNum; + + @Column(name = "title", nullable = false) + private String title; + + @Column(name = "description", nullable = false, columnDefinition = "TEXT") + private String description; +} diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java b/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java new file mode 100644 index 0000000..0079df6 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java @@ -0,0 +1,9 @@ +package com.umc.hackaton.snapspot.spot.repository; + +import com.umc.hackaton.snapspot.spot.entity.Spot; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface SpotRepository extends JpaRepository { +} diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java b/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java new file mode 100644 index 0000000..dc87610 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java @@ -0,0 +1,36 @@ +package com.umc.hackaton.snapspot.spot.service; + +import com.umc.hackaton.snapspot.spot.dto.SpotDto; +import com.umc.hackaton.snapspot.spot.dto.SpotRequestDto; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.user.entity.User; +import com.umc.hackaton.snapspot.spot.repository.SpotRepository; +import com.umc.hackaton.snapspot.user.repository.UserRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class SpotService { + private final SpotRepository spotRepository; + private final UserRepository userRepository; + + @Transactional + public void upload(SpotRequestDto dto) { + + User user = userRepository.findUserById(dto.getUserId()); + + SpotDto spotDto = new SpotDto(); + spotDto.setUser(user); + spotDto.setLongitude(dto.getLongitude()); + spotDto.setLatitude(dto.getLatitude()); + spotDto.setTitle(dto.getTitle()); + spotDto.setDescription(dto.getDescription()); + spotDto.setImgUrl(dto.getImgUrl()); + + spotRepository.save(spotDto.toEntity()); + + } +} 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 2ca3c9e..bdf74fb 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 @@ -7,4 +7,7 @@ public interface UserRepository extends JpaRepository { Optional findByUsername(String username); + Optional findByUserId(String userId); + User findUserById(Long id); + } From a660cece6d83f071510a5b6aacdbdff065672c50 Mon Sep 17 00:00:00 2001 From: do-dop Date: Thu, 4 Jul 2024 23:53:38 +0900 Subject: [PATCH 2/4] merge: dev --- .../umc/hackaton/snapspot/user/repository/UserRepository.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 bdf74fb..0b3cff5 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,8 +6,7 @@ import java.util.Optional; public interface UserRepository extends JpaRepository { - Optional findByUsername(String username); Optional findByUserId(String userId); - User findUserById(Long id); + User findUserById(Long id); } From 2187f9b9d3ff05cc3968360fadb89c53f22db830 Mon Sep 17 00:00:00 2001 From: do-dop Date: Thu, 4 Jul 2024 23:53:42 +0900 Subject: [PATCH 3/4] merge: dev --- .../snapspot/SnapspotApplication.java | 4 +- .../snapspot/user/service/UserService.java | 42 ++++--------------- 2 files changed, 9 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/umc/hackaton/snapspot/SnapspotApplication.java b/src/main/java/com/umc/hackaton/snapspot/SnapspotApplication.java index 6eb4861..fdb3504 100644 --- a/src/main/java/com/umc/hackaton/snapspot/SnapspotApplication.java +++ b/src/main/java/com/umc/hackaton/snapspot/SnapspotApplication.java @@ -2,10 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication -@EnableJpaAuditing +@EnableJpaRepositories public class SnapspotApplication { public static void main(String[] args) { 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 96a5080..6345606 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,22 +1,16 @@ package com.umc.hackaton.snapspot.user.service; -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.dto.UserRequestDto; import com.umc.hackaton.snapspot.user.entity.User; import com.umc.hackaton.snapspot.user.repository.UserRepository; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 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; @@ -24,35 +18,13 @@ 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()); - - } } From 82cd22cc962f5133d6b67ffc12955f2f3417879d Mon Sep 17 00:00:00 2001 From: do-dop Date: Fri, 5 Jul 2024 01:02:27 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20=EC=8A=A4=ED=8C=9F=20=EC=97=85?= =?UTF-8?q?=EB=A1=9C=EB=93=9C=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hackaton/snapspot/spot/entity/Spot.java | 8 ++-- .../user/repository/UserRepository.java | 2 +- .../snapspot/user/service/UserService.java | 42 +++++++++++++++---- 3 files changed, 40 insertions(+), 12 deletions(-) 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()); + + } }