Skip to content

Commit

Permalink
Merge pull request #11 from 2E1I/dev
Browse files Browse the repository at this point in the history
cicd test
  • Loading branch information
CEO-Nick authored Apr 18, 2024
2 parents ce051d3 + 08bd984 commit 7870175
Show file tree
Hide file tree
Showing 23 changed files with 295 additions and 147 deletions.
77 changes: 0 additions & 77 deletions .github/workflows/cicd.yml

This file was deleted.

34 changes: 20 additions & 14 deletions src/main/java/com/e2i1/linkeepserver/common/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,35 @@
@AllArgsConstructor
@Getter
public enum ErrorCode{
OK(HttpStatus.OK, "성공"),
BAD_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청"),
SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러"),
NULL_POINT(HttpStatus.INTERNAL_SERVER_ERROR, "Null point"),
NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않습니다."),
OK(200,HttpStatus.OK, "성공"),
BAD_REQUEST(400,HttpStatus.BAD_REQUEST, "잘못된 요청"),
SERVER_ERROR(500,HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러"),
NULL_POINT(500, HttpStatus.INTERNAL_SERVER_ERROR, "Null point"),
NOT_FOUND(404,HttpStatus.NOT_FOUND, "존재하지 않습니다."),

// 소셜 로그인 관련 에러 코드
UNKNOWN_OAUTH_LOGIN(HttpStatus.BAD_REQUEST, "알 수 없는 소셜 로그인 형식"),
UNKNOWN_OAUTH_LOGIN(14000, HttpStatus.BAD_REQUEST, "알 수 없는 소셜 로그인 형식"),

// 토큰 관련 에러 코드
INVALID_TOKEN(HttpStatus.BAD_REQUEST, "유효하지 않은 토큰"),
EXPIRED_TOKEN(HttpStatus.BAD_REQUEST, "만료된 토큰"),
TOKEN_EXCEPTION(HttpStatus.BAD_REQUEST, "토큰 알 수 없는 에러"),
AUTHORIZATION_TOKEN_NOT_FOUND(HttpStatus.BAD_REQUEST, "인증 헤더 토큰 없음"),
INVALID_TOKEN(24000,HttpStatus.BAD_REQUEST, "유효하지 않은 토큰"),
EXPIRED_TOKEN(24001,HttpStatus.BAD_REQUEST, "만료된 토큰"),
TOKEN_EXCEPTION(24002,HttpStatus.BAD_REQUEST, "토큰 알 수 없는 에러"),
AUTHORIZATION_TOKEN_NOT_FOUND(24003,HttpStatus.BAD_REQUEST, "인증 헤더 토큰 없음"),

// 유저 관련 에러 코드
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "사용자를 찾을 수 없음"),
USER_NOT_FOUND(34040,HttpStatus.NOT_FOUND, "사용자를 찾을 수 없음"),

// 모음집 관련 에러 코드
COLLECTION_NOT_FOUND(HttpStatus.NOT_FOUND, "모음집을 찾을 수 없음"),
;
COLLECTION_NOT_FOUND(44040,HttpStatus.NOT_FOUND, "모음집을 찾을 수 없음"),

// 링크 관련 에러 코드
LINK_NOT_FOUND(54040,HttpStatus.NOT_FOUND, "링크를 찾을 수 없음"),

// collaborator 관련 에러 코드
ACCESS_DENIED(64030,HttpStatus.FORBIDDEN, "해당 유저는 모음집에 접근할 수 없음"),
;
private final int errorCode;
private final HttpStatus httpStatusCode;
private final String description;

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.e2i1.linkeepserver.domain.collaborators.repository;

import com.e2i1.linkeepserver.domain.collaborators.entity.CollaboratorsEntity;
import org.springframework.data.repository.Repository;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CollaboratorsRepository extends Repository<CollaboratorsEntity, Long> {
public interface CollaboratorsRepository extends JpaRepository<CollaboratorsEntity, Long> {

Optional<CollaboratorsEntity> findByUserIdAndCollectionId(Long userId, Long collectionId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.e2i1.linkeepserver.domain.collaborators.service;

import com.e2i1.linkeepserver.common.error.ErrorCode;
import com.e2i1.linkeepserver.common.exception.ApiException;
import com.e2i1.linkeepserver.domain.collaborators.repository.CollaboratorsRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -9,4 +11,9 @@
public class CollaboratorsService {
private final CollaboratorsRepository collaboratorsRepository;

public void findByUserIdAndCollectionIdWithThrow(Long userId, Long collectionId) {
collaboratorsRepository.findByUserIdAndCollectionId(userId, collectionId)
.orElseThrow(() -> new ApiException(ErrorCode.ACCESS_DENIED));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,41 @@

import com.e2i1.linkeepserver.common.annotation.Business;
import com.e2i1.linkeepserver.domain.collections.converter.CollectionsConverter;
import com.e2i1.linkeepserver.domain.collections.dto.CollectionLinkDTO;
import com.e2i1.linkeepserver.domain.collections.dto.CollectionUserResDTO;
import com.e2i1.linkeepserver.domain.collections.entity.CollectionsEntity;
import com.e2i1.linkeepserver.domain.collections.service.CollectionsService;
import com.e2i1.linkeepserver.domain.links.converter.LinksConverter;
import com.e2i1.linkeepserver.domain.links.entity.LinksEntity;
import com.e2i1.linkeepserver.domain.links.service.LinksService;
import com.e2i1.linkeepserver.domain.tags.entity.TagsEntity;
import com.e2i1.linkeepserver.domain.tags.service.TagsService;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Business
@RequiredArgsConstructor
public class CollectionsBusiness {
private final CollectionsService collectionsService;
private final CollectionsConverter collectionsConverter;

private final CollectionsService collectionsService;
private final LinksService linksService;
private final TagsService tagsService;
private final CollectionsConverter collectionsConverter;
private final LinksConverter linksConverter;

public CollectionUserResDTO getCollection(Long id) {
CollectionsEntity collection = collectionsService.findByIdWithThrow(id);
List<LinksEntity> linkList = linksService.findByCollections(collection);
List<CollectionLinkDTO> linkDTOList = linkList.stream().map(linksConverter::toCollectionLinkDTO)
.toList();
List<TagsEntity> tagList = tagsService.findByCollection(collection);
List<String> tagDTOList = tagList.stream().map(TagsEntity::getTagName)
.collect(Collectors.toList());
CollectionUserResDTO result = collectionsConverter.toCollectionUserResDTO(collection,
linkDTOList, tagDTOList);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,23 @@ public ResponseEntity<List<CollectionTitleResDTO>> getCollectionTitle(){
}

@GetMapping("/collections/{collectionId}")
public ResponseEntity<CollectionUserResDTO> getCollection(@PathVariable String collectionId){
public ResponseEntity<CollectionUserResDTO> getCollection(@PathVariable Long collectionId){

return ResponseEntity.ok(null);
return ResponseEntity.ok(collectionsBusiness.getCollection(collectionId));

}

@GetMapping("/collections/like")
public ResponseEntity<HashMap<String,Long>> getUserLikeCollectionList(){
@PostMapping("/collections/like")
public ResponseEntity<HashMap<String,Long>> countLike(@RequestBody Long collectionId){
HashMap<String,Long> result = new HashMap<>();
result.put("numOfLikes", 22L);
Long numOfLikes = 5L;
result.put("numOfLikes", numOfLikes);
return ResponseEntity.ok(result);
}

@PostMapping("/collections/like")
public ResponseEntity<CollectionResDTO> countLike(@RequestBody Long collectionId){
@GetMapping("/collections/like")
public ResponseEntity<CollectionResDTO> getUserLikeCollectionList(){

return ResponseEntity.ok(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@

import com.e2i1.linkeepserver.common.annotation.Converter;
import com.e2i1.linkeepserver.domain.collections.business.CollectionsBusiness;
import com.e2i1.linkeepserver.domain.collections.dto.CollectionLinkDTO;
import com.e2i1.linkeepserver.domain.collections.dto.CollectionUserResDTO;
import com.e2i1.linkeepserver.domain.collections.entity.CollectionsEntity;
import com.e2i1.linkeepserver.domain.links.entity.LinksEntity;
import com.e2i1.linkeepserver.domain.tags.entity.TagsEntity;
import lombok.RequiredArgsConstructor;

import java.util.List;

@Converter
@RequiredArgsConstructor
public class CollectionsConverter {

public CollectionUserResDTO toCollectionUserResDTO(CollectionsEntity collection, List<CollectionLinkDTO> linkList, List<String> tagsList){
CollectionUserResDTO userResDTO = CollectionUserResDTO.builder()
.title(collection.getTitle())
.imgUrl(collection.getImgURL())
.description(collection.getDescription())
.linkList(linkList)
.tagList(tagsList)
.build();
return userResDTO;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Data
Expand All @@ -19,5 +20,5 @@ public class CollectionLinkDTO {
private Long numOfViews;
private String title;
private String description;
private LocalDate updatedAt;
private LocalDateTime updatedAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class CollectionsService {
private final CollaboratorsRepository collaboratorsRepository;
private final CollectionsRepository collectionsRepository;

public CollectionsEntity findById(Long collectionId) {
public CollectionsEntity findByIdWithThrow(Long collectionId) {
return collectionsRepository.findById(collectionId)
.orElseThrow(() -> {
log.error("Collection Not Found!! collectionId = {}", collectionId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,73 @@
package com.e2i1.linkeepserver.domain.links.business;

import com.e2i1.linkeepserver.common.annotation.Business;
import com.e2i1.linkeepserver.domain.collaborators.service.CollaboratorsService;
import com.e2i1.linkeepserver.domain.collections.entity.CollectionsEntity;
import com.e2i1.linkeepserver.domain.collections.service.CollectionsService;
import com.e2i1.linkeepserver.domain.links.converter.LinksConverter;
import com.e2i1.linkeepserver.domain.links.dto.LinkReqDTO;
import com.e2i1.linkeepserver.domain.links.dto.LinkResDTO;
import com.e2i1.linkeepserver.domain.links.dto.SearchLinkResDTO;
import com.e2i1.linkeepserver.domain.links.entity.LinksEntity;
import com.e2i1.linkeepserver.domain.links.service.LinksService;
import com.e2i1.linkeepserver.domain.users.entity.UsersEntity;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

@Business
@RequiredArgsConstructor
public class LinksBusiness {

private final LinksService linksService;
private final LinksConverter linksConverter;

private final CollectionsService collectionsService;

private final CollaboratorsService collaboratorsService;

/**
* link 저장하기
* link 저장하기 해당 컬렉션에 권한 있는지 확인 후 저장하기
*/
public LinksEntity save(LinkReqDTO req) {
CollectionsEntity collection = collectionsService.findById(req.getCollectionId());
LinksEntity linkEntity = linksConverter.toEntity(req, collection);
public LinksEntity save(LinkReqDTO req, UsersEntity user) {
/*
내가 해당 collection의 작업자일 때만 링크 저장 가능
만약 해당 collection이 존재하지 않거나, 내가 collection의 작업자가 아니라면 예외가 발생한다
*/
CollectionsEntity collection = collectionsService.findByIdWithThrow(req.getCollectionId());
collaboratorsService.findByUserIdAndCollectionIdWithThrow(user.getId(), collection.getId());

LinksEntity linkEntity = linksConverter.toEntity(req, collection, user);
return linksService.save(linkEntity);
}

/**
* link 단건 조회 현재 로그인된 유저가 생성한 link가 아닌 경우, link 조회수 1증가 즉, 자기가 만든 link는 아무리 조회해도 조회수 증가 안함
*/
@Transactional
public LinkResDTO findOneById(Long linkId, Long userId) {
LinksEntity link = linksService.findOneByIdAndUserId(linkId);

// 현재 로그인된 유저의 링크가 아닐 경우에만 조회 수 증가
if (!userId.equals(link.getUser().getId())) {
// link 객체는 linksService를 통해서 가져온거라 현재 영속성 컨텍스트 안에 있음
// 이렇게 수정만해도 transaction 끝날 때, dirty checking을 통해 자동으로 DB에 수정사항 반영된다.
link.updateView();
}
return linksConverter.toResponse(link);
}

// TODO : 검색어를 공백 기준으로 나누고 이들이 모두 존재하는 레코드들을 리턴하도록 메서드 수정하기

/**
* 링크 title, description을 조회해 해당 검색어 들어있는 링크 목록 가져오기
*/
public List<SearchLinkResDTO> searchLinks(String searchTerm) {
List<LinksEntity> linkList = linksService.searchLinks(searchTerm);

return linkList.stream()
.map(linksConverter::toSearchResponse)
.collect(Collectors.toList());
}
}
Loading

0 comments on commit 7870175

Please sign in to comment.