Skip to content

Commit

Permalink
feat : create "create Party" api #10
Browse files Browse the repository at this point in the history
  • Loading branch information
LWJ0513 committed Mar 18, 2023
1 parent a1471a7 commit 4afc7cf
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.ft.modumoa.controller;

import com.ft.modumoa.config.auth.PrincipalDetails;
import com.ft.modumoa.dto.PartyCreateResponseDTO;
import com.ft.modumoa.dto.PartyListDTO;
import com.ft.modumoa.dto.PartyCreateRequestDTO;
import com.ft.modumoa.service.PartyService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
Expand All @@ -20,4 +25,10 @@ public class PartyController {
public List<PartyListDTO> getPartyList(){
return partyService.getAllPartyList();
}

@PostMapping("/party/create")
public PartyCreateResponseDTO createParty(@RequestBody PartyCreateRequestDTO partyRequestDTO, @AuthenticationPrincipal PrincipalDetails user) {

return partyService.createParty(partyRequestDTO, user.getUser());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ft.modumoa.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;

import java.time.LocalDateTime;

@Data
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class PartyCreateRequestDTO {

private String title;
private String content;
private String category;
private int max;
private LocalDateTime dueDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ft.modumoa.dto;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class PartyCreateResponseDTO {
private Long id;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ft.modumoa.entity;

import lombok.Builder;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;

Expand All @@ -8,6 +9,7 @@
import java.time.LocalDateTime;

@Data
@Builder
@Entity
public class Party {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.ft.modumoa.repository;

import com.ft.modumoa.entity.Category;
import com.ft.modumoa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CategoryRepository extends JpaRepository<Category, Long> {
public Category findByType(String type);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.ft.modumoa.service;

import com.ft.modumoa.dto.PartyCreateResponseDTO;
import com.ft.modumoa.dto.PartyListDTO;
import com.ft.modumoa.dto.PartyCreateRequestDTO;
import com.ft.modumoa.entity.Party;
import com.ft.modumoa.entity.User;
import com.ft.modumoa.repository.CategoryRepository;
import com.ft.modumoa.repository.PartyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -16,15 +20,29 @@ public class PartyService {
@Autowired
private PartyRepository partyRepository;

public List<PartyListDTO> getAllPartyList(){
@Autowired
private CategoryRepository categoryRepository;

public List<PartyListDTO> getAllPartyList() {

return partyRepository.findAll()
.stream()
.map(this::convertEntityToDto)
.collect(Collectors.toList());
}

private PartyListDTO convertEntityToDto(Party party){
public PartyCreateResponseDTO createParty(PartyCreateRequestDTO partyRequestDTO, User user) {

Party party = convertDtoToEntity(partyRequestDTO, user);

party = partyRepository.save(party);

return PartyCreateResponseDTO.builder()
.id(party.getId())
.build();
}

private PartyListDTO convertEntityToDto(Party party) {

return PartyListDTO.builder()
.id(party.getId())
Expand All @@ -38,4 +56,17 @@ private PartyListDTO convertEntityToDto(Party party){
.status(LocalDateTime.now().isBefore(party.getDeadline()))
.build();
}

private Party convertDtoToEntity(PartyCreateRequestDTO partyRequestDTO, User user){
Party party = Party.builder()
.writer(user)
.category(categoryRepository.findByType(partyRequestDTO.getCategory()))
.title(partyRequestDTO.getTitle())
.content(partyRequestDTO.getContent())
.max(partyRequestDTO.getMax())
.current(0)
.create_at(LocalDateTime.now())
.deadline(partyRequestDTO.getDueDate())
.build();
}
}

0 comments on commit 4afc7cf

Please sign in to comment.