Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[✨3주차 실습 과제 제출✨] #8

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.sopt.carrotMarket.domain.address.entity;


import com.sopt.carrotMarket.global.BaseTimeEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PRIVATE;
import static lombok.AccessLevel.PROTECTED;

@Getter
@NoArgsConstructor(access = PROTECTED)
@Entity
@Builder
@AllArgsConstructor(access = PRIVATE)
public class Address extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "address_id")
private Long id;

@Column(nullable = false)
private String city; //시

@Column(nullable = false)
private String district; //구

@Column(nullable = false)
private String town; //동

@Column(nullable = false)
private Float longitude;

@Column(nullable = false)
private Float latitude;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.sopt.carrotMarket.domain.address.entity;

import jakarta.persistence.Embeddable;

@Embeddable
public class DesireAddress {
private Address desireAddress;
private String desirePlaceName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.sopt.carrotMarket.domain.item.controller;

import com.sopt.carrotMarket.domain.item.service.ItemService;
import com.sopt.carrotMarket.domain.item.service.dto.ItemPostRequest;
import com.sopt.carrotMarket.global.ApiUtils;
import com.sopt.carrotMarket.global.CommonResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
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;

@RestController
@RequiredArgsConstructor
@RequestMapping("/item/post")
@Slf4j

public class ItemController {
private final ItemService itemService;

@PostMapping
public CommonResponse createPost(@Validated @RequestBody ItemPostRequest itemPostRequest) {
itemService.createPost(itemPostRequest);
return ApiUtils.success(200, HttpStatus.OK, itemPostRequest);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.sopt.carrotMarket.domain.item.entity;


import com.sopt.carrotMarket.domain.address.entity.DesireAddress;
import com.sopt.carrotMarket.domain.user.entity.User;
import com.sopt.carrotMarket.global.BaseTimeEntity;
import com.sopt.carrotMarket.global.enums.ItemCategory;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static jakarta.persistence.EnumType.STRING;
import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PRIVATE;
import static lombok.AccessLevel.PROTECTED;

@Getter
@NoArgsConstructor(access = PROTECTED)
@Entity
@Builder
@AllArgsConstructor(access = PRIVATE)
public class Item extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "post_id")
private Long id;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;

@Column(nullable = false)
private String title;

@Column(columnDefinition = "longtext", nullable = false)
private String content;

@Enumerated(STRING)
@Column(nullable = false)
private ItemCategory category;

private long price;

@Embedded
private DesireAddress desireAddress;

private long view;
}







Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.sopt.carrotMarket.domain.item.entity;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ItemRepository extends JpaRepository<Item, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.sopt.carrotMarket.domain.item.service;

import com.sopt.carrotMarket.domain.item.entity.ItemRepository;
import com.sopt.carrotMarket.domain.item.service.dto.ItemPostRequest;
import com.sopt.carrotMarket.domain.user.repository.UserRepository;
import com.sopt.carrotMarket.global.enums.ItemCategory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
@RequiredArgsConstructor
public class ItemService {

private final UserRepository userRepository;
private final ItemRepository postRepository;
public void createPost(ItemPostRequest request) {
// ItemCategory postCategory = findCategory(request.getCategory());
// Item post = request.toEntity(a, postCategory);
// postRepository.save(post);

}

public ItemCategory findCategory(int category) {
ItemCategory categoryType = Arrays.stream(ItemCategory.values())
.filter(c -> c.getValue() == category)
.findAny().get();
return categoryType;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sopt.carrotMarket.domain.item.service.dto;

public record ItemPostRequest(
String title,
String description,
int price
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.sopt.carrotMarket.domain.trade_history.entity;


import com.sopt.carrotMarket.domain.item.entity.Item;
import com.sopt.carrotMarket.domain.user.entity.User;
import com.sopt.carrotMarket.global.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PRIVATE;
import static lombok.AccessLevel.PROTECTED;

@Getter
@NoArgsConstructor(access = PROTECTED)
@Entity
@Builder
@AllArgsConstructor(access = PRIVATE)
public class TradeHistory extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "trade_history_id")
private Long id;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "post_id", nullable = false)
private Item item;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "buyer_id", nullable = false)
private User buyer;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "seller_id", nullable = false)
private User seller;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.sopt.carrotMarket.domain.user.controller;


import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class UserController {
// private
//
//
// @PostMapping("/api/auth/signup")
// public String getUserInfo() {
//
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.sopt.carrotMarket.domain.user.entity;

import com.sopt.carrotMarket.domain.address.entity.Address;
import com.sopt.carrotMarket.global.BaseTimeEntity;
import com.sopt.carrotMarket.global.enums.UserStatus;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PRIVATE;
import static lombok.AccessLevel.PROTECTED;

@Getter
@NoArgsConstructor(access = PROTECTED)
@Entity
@Table(name="users")
@Builder
@AllArgsConstructor(access = PRIVATE)
public class User extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "user_id")
private Long id;

@Column(nullable = false)
private String nickname;

@Column(nullable = false)
private String email;

@Column(nullable = false)
private String password;

@Column(nullable = false)
private String phoneNumber;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "address_id")
private Address address;

@Enumerated(EnumType.STRING)
private UserStatus status;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sopt.carrotMarket.domain.user.repository;

import com.sopt.carrotMarket.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<Long, User> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.sopt.carrotMarket.domain.user.service;

public interface UserService {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.sopt.carrotMarket.domain.user.service;

import org.apache.catalina.User;

public class UserServiceImpl implements UserService {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.sopt.carrotMarket.global;

import org.springframework.http.HttpStatus;

public class ApiUtils {
public static <T> CommonResponse<T> success(int code, HttpStatus httpStatus, T result) {
return new CommonResponse<>(code, httpStatus, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.sopt.carrotMarket.global;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@Getter
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public abstract class BaseTimeEntity {

@CreatedDate
@Column(name = "created_at", nullable = false)
private LocalDateTime createdAt;

@LastModifiedDate
@Column(name = "modified_at", nullable = false)
private LocalDateTime modifiedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.sopt.carrotMarket.global;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Builder;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
public class CommonResponse<T> {
private final int code;
private final HttpStatus httpStatus;
private final String message;

@JsonInclude(JsonInclude.Include.NON_NULL)
private final T data;

@Builder
public CommonResponse(int code, HttpStatus httpStatus, T data) {
this.code = code;
this.httpStatus = httpStatus;
this.message = "요청에 성공했습니다";
this.data = data;
}
}
Loading