Skip to content

Commit

Permalink
Merge pull request #41 from Team-UMC/feature/#36/TIL
Browse files Browse the repository at this point in the history
[FEAT] TIL(Today I Learned) API 구현
  • Loading branch information
junseokkim authored Feb 4, 2024
2 parents 822f682 + 3533f79 commit a523490
Show file tree
Hide file tree
Showing 14 changed files with 416 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.umc.networkingService.domain.todayILearned.controller;

import com.umc.networkingService.config.security.auth.CurrentMember;
import com.umc.networkingService.domain.member.entity.Member;
import com.umc.networkingService.domain.todayILearned.dto.requeest.TodayILearnedRequest;
import com.umc.networkingService.domain.todayILearned.dto.requeest.TodayILearnedRequest.TodayILearnedCreate;
import com.umc.networkingService.domain.todayILearned.dto.requeest.TodayILearnedRequest.TodayILearnedUpdate;
import com.umc.networkingService.domain.todayILearned.dto.response.TodayILearnedResponse.TodayILearnedId;
import com.umc.networkingService.domain.todayILearned.dto.response.TodayILearnedResponse.TodayILearnedInfo;
import com.umc.networkingService.domain.todayILearned.dto.response.TodayILearnedResponse.TodayILearnedInfos;
import com.umc.networkingService.domain.todayILearned.service.TodayILearnedService;
import com.umc.networkingService.global.common.base.BaseResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "Today I Learned API", description = "Today I Learned 관련 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/today-i-learned")
public class TodayILearnedController {

private final TodayILearnedService todayILearnedService;

// POST
@Operation(summary = "Today I Learned 추가", description = "TIL을 추가하는 API입니다.")
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "성공")
})
public BaseResponse<TodayILearnedId> createTodayILearned(@CurrentMember Member member,
@RequestPart(value = "files", required = false) List<MultipartFile> files,
@RequestPart("request") TodayILearnedCreate request) {

return BaseResponse.onSuccess(todayILearnedService.createTodayILearned(member, files, request));
}

@Operation(summary = "Today I Learned 수정", description = "TIL를 수정하는 API입니다.")
@PostMapping(value = {"update/{todayILearnedId}"}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "성공")
})
public BaseResponse<TodayILearnedId> updateTodayILearned(@CurrentMember Member member,
@PathVariable("todayILearnedId") UUID todayILearnedId,
@RequestPart(value = "files", required = false) List<MultipartFile> files,
@RequestPart("request") TodayILearnedUpdate request) {

return BaseResponse.onSuccess(todayILearnedService.updateTodayILearned(member, todayILearnedId, files, request));
}

//GET
@Operation(summary = "Today I Learned 조회(일별)", description = "TIL(일별)을 조회하는 API입니다.")
@GetMapping
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "성공")
})
public BaseResponse<TodayILearnedInfos> getTodayILearnedInfos(@CurrentMember Member member) {

return BaseResponse.onSuccess(todayILearnedService.getTodayILearnedInfos(member));
}

// DELETE
@Operation(summary = "Today I Learned 삭제", description = "TIL을 삭제하는 API입니다.")
@DeleteMapping("/{todayILearnedId}")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "성공")
})
public BaseResponse<TodayILearnedId> deleteTodayILearned(@CurrentMember Member member,
@PathVariable("todayILearnedId") UUID todayILearnedId) {
return BaseResponse.onSuccess(todayILearnedService.deleteTodayILearned(member, todayILearnedId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.umc.networkingService.domain.todayILearned.dto.requeest;

import com.umc.networkingService.global.common.enums.Part;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

public class TodayILearnedRequest {

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public static class TodayILearnedCreate {
private Part part;
private String title;
private String subTitle;
private String content;
}

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public static class TodayILearnedUpdate {
private String title;
private String subTitle;
private String content;
private Part part;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.umc.networkingService.domain.todayILearned.dto.response;

import com.umc.networkingService.global.common.enums.Part;
import java.util.List;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

public class TodayILearnedResponse {

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public static class TodayILearnedId {
private UUID todayILearnedId;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class TodayILearnedInfo {
private UUID todayILearnedId;
private String title;
private String subTitle;
private Part part;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class TodayILearnedInfos {
private List<TodayILearnedInfo> todayILearnedInfos;
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.umc.networkingService.domain.todayILearned.entity;

import com.umc.networkingService.domain.member.entity.Member;
import com.umc.networkingService.domain.todayILearned.dto.requeest.TodayILearnedRequest.TodayILearnedUpdate;
import com.umc.networkingService.global.common.base.BaseEntity;
import com.umc.networkingService.global.common.enums.Part;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLRestriction;
Expand All @@ -13,8 +16,10 @@
import java.util.UUID;

@Getter
@Builder
@Entity
@NoArgsConstructor(access= AccessLevel.PROTECTED)
@AllArgsConstructor
@SQLRestriction("deleted_at is null")
@Table(name = "today_i_learned")
public class TodayILearned extends BaseEntity {
Expand All @@ -39,5 +44,12 @@ public class TodayILearned extends BaseEntity {
@Column(nullable = false)
private Part part;

private Boolean linkedNotion;
public void updateTodayILearned(TodayILearnedUpdate request) {
this.title = request.getTitle();
this.subtitle = request.getSubTitle();
this.content = request.getContent();
this.part = request.getPart();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.umc.networkingService.global.common.base.BaseEntity;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLRestriction;
Expand All @@ -10,9 +12,11 @@
import java.util.UUID;

@Getter
@Builder
@Entity
@Table(name = "today_i_learned_file")
@NoArgsConstructor
@AllArgsConstructor
@SQLRestriction("deleted_at is null")
public class TodayILearnedFile extends BaseEntity {

Expand All @@ -21,9 +25,6 @@ public class TodayILearnedFile extends BaseEntity {
@Column(name = "today_i_learned_file_id")
private UUID id;

@Column(nullable = false)
private String name;

@Column(nullable = false)
private String url;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.umc.networkingService.domain.todayILearned.mapper;

import com.umc.networkingService.domain.todayILearned.entity.TodayILearned;
import com.umc.networkingService.domain.todayILearned.entity.TodayILearnedFile;
import org.springframework.stereotype.Component;

@Component
public class TodayILearnedFileMapper {
public TodayILearnedFile toTodayILearnedFile(TodayILearned todayILearned, String url) {
return TodayILearnedFile.builder()
.todayILearned(todayILearned)
.url(url)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.umc.networkingService.domain.todayILearned.mapper;

import com.umc.networkingService.domain.member.entity.Member;
import com.umc.networkingService.domain.todayILearned.dto.requeest.TodayILearnedRequest.TodayILearnedCreate;
import com.umc.networkingService.domain.todayILearned.dto.requeest.TodayILearnedRequest.TodayILearnedUpdate;
import com.umc.networkingService.domain.todayILearned.dto.response.TodayILearnedResponse.TodayILearnedId;
import com.umc.networkingService.domain.todayILearned.dto.response.TodayILearnedResponse.TodayILearnedInfo;
import com.umc.networkingService.domain.todayILearned.dto.response.TodayILearnedResponse.TodayILearnedInfos;
import com.umc.networkingService.domain.todayILearned.entity.TodayILearned;
import com.umc.networkingService.domain.todayILearned.entity.TodayILearnedFile;
import java.util.List;
import java.util.UUID;
import org.springframework.stereotype.Component;

@Component
public class TodayILearnedMapper {

public TodayILearned toTodayILearned(Member member, TodayILearnedCreate request) {
return TodayILearned.builder()
.writer(member)
.title(request.getTitle())
.subtitle(request.getSubTitle())
.content(request.getContent())
.part(request.getPart())
.build();
}

public TodayILearnedId toTodayILearnedId(UUID todayILearnedId) {
return TodayILearnedId.builder()
.todayILearnedId(todayILearnedId)
.build();
}

public TodayILearnedInfo toTodayILearnedInfo(TodayILearned todayILearned) {
return TodayILearnedInfo.builder()
.todayILearnedId(todayILearned.getId())
.title(todayILearned.getTitle())
.subTitle(todayILearned.getSubtitle())
.part(todayILearned.getPart())
.build();
}

public TodayILearnedInfos toTodayILearnedInfos(List<TodayILearnedInfo> todayILearnedInfos) {
return TodayILearnedInfos.builder()
.todayILearnedInfos(todayILearnedInfos)
.build();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.umc.networkingService.domain.todayILearned.repository;

import com.umc.networkingService.domain.todayILearned.entity.TodayILearnedFile;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TodayILearnedFileRepository extends JpaRepository<TodayILearnedFile, UUID> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.umc.networkingService.domain.todayILearned.repository;

import com.umc.networkingService.domain.member.entity.Member;
import com.umc.networkingService.domain.todayILearned.entity.TodayILearned;
import io.lettuce.core.dynamic.annotation.Param;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface TodayILearnedRepository extends JpaRepository<TodayILearned, UUID> {

@Query(value = "SELECT t FROM TodayILearned t WHERE (t.writer = :writer AND DATE(t.createdAt) = :date)")
List<TodayILearned> findTodayILearnedByWriterAndCreateDate(@Param("writer") Member writer,
@Param("date") LocalDate date);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.umc.networkingService.domain.todayILearned.service;

import com.umc.networkingService.domain.todayILearned.entity.TodayILearned;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;

public interface TodayILearnedFileService {
void uploadTodayILearnedFiles(TodayILearned todayILearned, List<MultipartFile> files);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.umc.networkingService.domain.todayILearned.service;

import com.umc.networkingService.domain.todayILearned.entity.TodayILearned;
import com.umc.networkingService.domain.todayILearned.mapper.TodayILearnedFileMapper;
import com.umc.networkingService.domain.todayILearned.repository.TodayILearnedFileRepository;
import com.umc.networkingService.domain.todayILearned.repository.TodayILearnedRepository;
import com.umc.networkingService.global.utils.S3FileComponent;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class TodayILearnedFileServiceImpl implements TodayILearnedFileService {

private final TodayILearnedFileRepository todayILearnedFileRepository;
private final TodayILearnedFileMapper todayILearnedFileMapper;
private final S3FileComponent s3FileComponent;

// TodayILearned 파일 업로드
@Override
@Transactional
public void uploadTodayILearnedFiles(TodayILearned todayILearned, List<MultipartFile> files) {
files.forEach(file -> {
todayILearnedFileRepository.save(todayILearnedFileMapper.toTodayILearnedFile(todayILearned,
s3FileComponent.uploadFile("TodayILearned", file)));
});

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.umc.networkingService.domain.todayILearned.service;

import com.umc.networkingService.domain.member.entity.Member;
import com.umc.networkingService.domain.todayILearned.dto.requeest.TodayILearnedRequest.TodayILearnedCreate;
import com.umc.networkingService.domain.todayILearned.dto.requeest.TodayILearnedRequest.TodayILearnedUpdate;
import com.umc.networkingService.domain.todayILearned.dto.response.TodayILearnedResponse.TodayILearnedId;
import com.umc.networkingService.domain.todayILearned.dto.response.TodayILearnedResponse.TodayILearnedInfo;
import com.umc.networkingService.domain.todayILearned.dto.response.TodayILearnedResponse.TodayILearnedInfos;
import java.util.List;
import java.util.UUID;
import org.springframework.web.multipart.MultipartFile;

public interface TodayILearnedService {
TodayILearnedId createTodayILearned(Member member, List<MultipartFile> files, TodayILearnedCreate request);
TodayILearnedInfos getTodayILearnedInfos(Member member);

TodayILearnedId updateTodayILearned(Member member, UUID todayILearnedId, List<MultipartFile> files, TodayILearnedUpdate request);

TodayILearnedId deleteTodayILearned(Member member, UUID todayILearnedId);
}
Loading

0 comments on commit a523490

Please sign in to comment.