Skip to content

Commit

Permalink
Merge pull request #22 from UMC-HACKATHON-SnapSpot/feat/#18
Browse files Browse the repository at this point in the history
[feat][스팟 생성]
  • Loading branch information
m3k0813 authored Jul 4, 2024
2 parents 055219d + 82cd22c commit d0bbd20
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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("스팟 업로드에 실패하였습니다.");
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotDto.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.umc.hackaton.snapspot.spot.dto;

public class SpotResponseDto {
}
49 changes: 49 additions & 0 deletions src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java
Original file line number Diff line number Diff line change
@@ -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")
private Long likeNum = 0L;

@Column(name = "visit_num")
private Long visitNum =0L;

@Column(name = "title", nullable = false)
private String title;

@Column(name = "description", nullable = false, columnDefinition = "TEXT")
private String description;
}
Original file line number Diff line number Diff line change
@@ -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<Spot, Long> {
}
Original file line number Diff line number Diff line change
@@ -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());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByUsername(String username);

User findUserById(Long id);
}

0 comments on commit d0bbd20

Please sign in to comment.