Skip to content

Commit

Permalink
feat: 푸시 토큰 저장 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jihoon-jang committed Oct 9, 2023
1 parent fb2fdbc commit 17bd3f8
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.polzzak.domain.pushtoken.controller;

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;

import com.polzzak.domain.pushtoken.model.CreatePushToken;
import com.polzzak.domain.pushtoken.service.PushTokenService;
import com.polzzak.global.common.ApiResponse;
import com.polzzak.global.security.LoginId;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/push-token")
public class PushTokenController {

private final PushTokenService pushTokenService;

@PostMapping
public ResponseEntity<ApiResponse<Void>> createStampBoard(
@LoginId Long memberId, @RequestBody @Valid CreatePushToken createPushToken
) {
pushTokenService.addToken(memberId, createPushToken.token());
return ResponseEntity.status(HttpStatus.CREATED).body(ApiResponse.created());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.polzzak.domain.pushtoken.model;

public record CreatePushToken(String token) {
}
34 changes: 34 additions & 0 deletions src/main/java/com/polzzak/domain/pushtoken/model/PushToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.polzzak.domain.pushtoken.model;

import com.polzzak.domain.model.BaseEntity;
import com.polzzak.domain.user.entity.Member;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@NoArgsConstructor
@Table(name = "push_token")
public class PushToken extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@Column(nullable = false)
private String token;

@Builder(builderMethodName = "createPushToken")
public PushToken(Member member, String token) {
this.member = member;
this.token = token;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.polzzak.domain.pushtoken.repository;

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

import com.polzzak.domain.pushtoken.model.PushToken;

public interface PushTokenRepository extends JpaRepository<PushToken, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.polzzak.domain.pushtoken.service;

import org.springframework.stereotype.Service;

import com.polzzak.domain.pushtoken.model.PushToken;
import com.polzzak.domain.pushtoken.repository.PushTokenRepository;
import com.polzzak.domain.user.entity.Member;
import com.polzzak.domain.user.service.UserService;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class PushTokenService {

private final UserService userService;
private final PushTokenRepository pushTokenRepository;

public void addToken(Long memberId, String token) {
Member member = userService.findMemberByMemberId(memberId);

PushToken pushToken = PushToken.createPushToken()
.member(member)
.token(token)
.build();

pushTokenRepository.save(pushToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.polzzak.domain.pushtoken.controller;

import static com.polzzak.support.TokenFixtures.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.restdocs.headers.HeaderDocumentation.*;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import com.polzzak.domain.pushtoken.service.PushTokenService;
import com.polzzak.domain.user.service.UserService;
import com.polzzak.support.PushTokenFixtures;
import com.polzzak.support.test.ControllerTestHelper;

@WebMvcTest(PushTokenController.class)
class PushTokenControllerTest extends ControllerTestHelper {

@MockBean
private PushTokenService pushTokenService;
@MockBean
private UserService userService;

private static final String BASE_URL = "/api/v1/push-token";

@BeforeEach
public void setup() {
when(userService.findMemberByMemberId(anyLong())).thenReturn(PushTokenFixtures.MEMBER);
}

@Test
@DisplayName("푸시 토큰 생성 테스트")
void createStampBoardTest() throws Exception {
doNothing().when(pushTokenService).addToken(anyLong(), anyString());

mockMvc.perform(
post(BASE_URL)
.header(HttpHeaders.AUTHORIZATION, TOKEN_TYPE + USER_ACCESS_TOKEN)
.contentType(MediaType.APPLICATION_JSON)
.content(objectToString(PushTokenFixtures.CREATE_PUSH_TOKEN)))
.andDo(print())
.andExpect(status().isCreated())
.andDo(document("push/push_token-create-success",
requestHeaders(
headerWithName(HttpHeaders.AUTHORIZATION).description("엑세스 토큰")
),
requestFields(
fieldWithPath("token").description("푸시 토큰")
),
responseFields(
fieldWithPath("code").description("응답 코드"),
fieldWithPath("messages").description("응답 메시지").optional(),
fieldWithPath("data").description("응답 데이터").optional()
)));
}
}
12 changes: 12 additions & 0 deletions src/test/java/com/polzzak/support/PushTokenFixtures.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.polzzak.support;

import com.polzzak.domain.membertype.entity.MemberType;
import com.polzzak.domain.membertype.entity.MemberTypeDetail;
import com.polzzak.domain.pushtoken.model.CreatePushToken;
import com.polzzak.domain.user.entity.Member;

public class PushTokenFixtures {

public static final Member MEMBER = new Member("NICK", "profileKEY", new MemberTypeDetail(MemberType.KID, "kid"));
public static final CreatePushToken CREATE_PUSH_TOKEN = new CreatePushToken("token");
}

0 comments on commit 17bd3f8

Please sign in to comment.