Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into dev
  • Loading branch information
SUMMERLOVE7 committed Feb 21, 2023
2 parents 1476d18 + 3ef05ac commit 1e29cc6
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.amatta.amatta_server.exception;

public class GifticonParseException extends RuntimeException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin(origins = {"https://amatta.site", "http://localhost:5173"}, allowCredentials = "true")
@RequestMapping("/fcm")
Expand All @@ -36,4 +35,9 @@ public ResponseEntity<?> sendTestMessage(@RequestParam("token") String token) {
}
return new ResponseEntity<>(HttpStatus.OK);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<?> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

@Getter
public class TokenRegisterDto {
@NotEmpty
@NotEmpty(message = "토큰이 존재해야 합니다")
private String token;

public TokenRegisterDto(String token) {
this.token = token;
}

public TokenRegisterDto() {

}
public TokenRegisterDto(){}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ void addToken(@Param("uid") long uid,
@Query("SELECT token FROM device_token WHERE uid IN " +
"(SELECT uid FROM gifticon WHERE expiresat BETWEEN (SELECT now()) AND :expirationThresholdDays AND usedat > (SELECT now()))")
List<String> findTokensByUidsOfGifticonsAboutToExpire(@Param("expirationThresholdDays") LocalDate date);

@Modifying
@Query("DELETE FROM device_token WHERE uid = :uid")
void deleteTokenByUid(@Param("uid") long uid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.amatta.amatta_server.exception.DuplicateGifticonException;
import com.amatta.amatta_server.exception.GifticonNotSupportedException;
import com.amatta.amatta_server.exception.GifticonParseException;
import com.amatta.amatta_server.exception.NotAuthenticatedException;
import com.amatta.amatta_server.fcm.service.FCMService;
import com.amatta.amatta_server.gifticon.dto.GifticonUseDto;
Expand Down Expand Up @@ -70,12 +71,12 @@ public ResponseEntity<?> notAuthenticatedExceptionHandler() {

@ExceptionHandler(GifticonNotSupportedException.class)
public ResponseEntity<?> gifticonNotSupportedExceptionHandler() {
return new ResponseEntity<>(GifticonNotSupportedException.message, HttpStatus.BAD_REQUEST);
return new ResponseEntity<>("", HttpStatus.OK);
}

@ExceptionHandler(IndexOutOfBoundsException.class)
public ResponseEntity<?> indexOutOfBoundExceptionHandler() {
return new ResponseEntity<>("잘못된 형식의 이미지입니다", HttpStatus.BAD_REQUEST);
@ExceptionHandler(GifticonParseException.class)
public ResponseEntity<?> gifticonParseExceptionHandler() {
return new ResponseEntity<>("", HttpStatus.OK);
}

@ExceptionHandler(DuplicateGifticonException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ public enum GifticonMapperEnum {
KAKAO(KakaoGifticonMapper.getInstance()) {
@Override
public boolean matches(List<String> texts) {
Pattern pattern = Pattern.compile("(?i)kakaotalk");
for(String text : texts) {
if(pattern.matcher(text).matches()) {
return true;
}
if(texts.get(texts.size()-1).matches("(?i)^kakaotalk.*")) {
return true;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.amatta.amatta_server.aop.ClassRequiresAuth;
import com.amatta.amatta_server.exception.DuplicateGifticonException;
import com.amatta.amatta_server.exception.GifticonNotSupportedException;
import com.amatta.amatta_server.exception.GifticonParseException;
import com.amatta.amatta_server.exception.NotAuthenticatedException;
import com.amatta.amatta_server.gifticon.dto.GifticonDto;
import com.amatta.amatta_server.gifticon.dto.GifticonImageDto;
Expand Down Expand Up @@ -65,7 +66,7 @@ public ResponseEntity<String> extractGifticonText(GifticonImageDto dto) {
);
}

public Gifticon mapTextToGifticon(GifticonTextDto dto) throws NullPointerException, IndexOutOfBoundsException, GifticonNotSupportedException {
public Gifticon mapTextToGifticon(GifticonTextDto dto) throws GifticonParseException, GifticonNotSupportedException {
GifticonMapper mapper = GifticonMapperFactory.getGifticonMapper(dto.getTexts());
return mapper.map(dto.getTexts());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.amatta.amatta_server.gifticon.util;

import com.amatta.amatta_server.exception.GifticonParseException;
import com.amatta.amatta_server.gifticon.model.Gifticon;

import java.sql.Date;
Expand All @@ -22,66 +23,30 @@ public static GifticonMapper getInstance() {
}

@Override
public Gifticon map(List<String> list) throws IndexOutOfBoundsException {
int brandNameIndex = list.lastIndexOf("교환처");
int expirationDateIndex = list.lastIndexOf("유효기간");
int orderIdIndex = list.lastIndexOf("주문번호");

String brandName = list.get(brandNameIndex+1);
String itemName = getItemName(list, brandName);
String barcode = getBarcode(list, brandNameIndex);

java.sql.Date expiretionDate = Date.valueOf(getExpirationDate(list, expirationDateIndex, orderIdIndex));

return Gifticon.builder()
.brandname(brandName)
.expiresat(expiretionDate)
.itemname(itemName)
.barcode(barcode)
.build();
}

private String getBarcode(List<String> list, int brandNameIndex) {
int barcodeStartIdx = 0;
for(int i = brandNameIndex - 1; i >= 0; i--) {
if(list.get(i).matches("^\\d{4}$")) {
barcodeStartIdx = i;
public Gifticon map(List<String> list) throws GifticonParseException {
try {
Date expireDate = Date.valueOf(LocalDate.parse(list.get(list.size() - 3)
.replace("유효기간", "").replace(" ", ""), DateTimeFormatter.ofPattern("yyyy년MM월dd일")));
String brandName = list.get(list.size() - 4).replace("교환처", "");
String barcode = list.get(list.size() - 5).replace(" ", "");

StringBuilder stringBuilder = new StringBuilder();
for (int i = list.size() - 6; !list.get(i).equals(brandName); i--) {
if (i == 0 && !list.get(i).equals(brandName)) {
stringBuilder.delete(0, stringBuilder.toString().length());
break;
}
stringBuilder.insert(0, list.get(i));
}
if(!list.get(i).matches("^\\d{4}$")) {
break;
}
}

StringBuilder st = new StringBuilder();
for(int i = barcodeStartIdx; i < brandNameIndex; i++) {
st.append(list.get(i));
return Gifticon.builder()
.brandname(brandName)
.expiresat(expireDate)
.itemname(stringBuilder.toString())
.barcode(barcode)
.build();
} catch(Exception e) {
throw new GifticonParseException();
}
return st.toString();
}

private LocalDate getExpirationDate(List<String> list, int expirationDateIndex, int orderIdIndex) throws IndexOutOfBoundsException {
StringBuilder expirationDate = new StringBuilder();
for(String date : list.subList(expirationDateIndex+1, orderIdIndex)) {
expirationDate.append(date);
}

return LocalDate.parse(expirationDate.toString(), DateTimeFormatter.ofPattern("yyyy년MM월dd일"));
}

private String getItemName(List<String> list, String brandName) throws IndexOutOfBoundsException {
int itemNameStartIndex = list.indexOf(brandName) + 1;
int itemNameEndIndex = 0;
for(int i = list.lastIndexOf(brandName) - 2; i >= 0; i--) {
if(!list.get(i).matches("^\\d{4}$")) {
itemNameEndIndex = i+1;
break;
}
}
StringBuilder itemName = new StringBuilder();
for(String name : list.subList(itemNameStartIndex, itemNameEndIndex)) {
itemName.append(name).append(" ");
}

return itemName.toString().trim();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static GifticonMapper getInstance() {
return instance;
}
@Override
public Gifticon map(List<String> list) throws IndexOutOfBoundsException, GifticonNotSupportedException {
public Gifticon map(List<String> list) throws GifticonNotSupportedException {
throw new GifticonNotSupportedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
Expand Down Expand Up @@ -64,6 +66,7 @@ public ResponseEntity<?> login(@Valid @RequestBody UserLoginReq userLoginReq, Ht
public ResponseEntity<?> logout(HttpServletRequest request) {
HttpSession session = request.getSession(false);
if (session != null) {
userService.logout();
session.invalidate();
}
return new ResponseEntity<>(new UserLogoutRes(true), HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package com.amatta.amatta_server.user.service;

import com.amatta.amatta_server.fcm.repository.DeviceTokenRepository;
import com.amatta.amatta_server.user.dto.*;
import com.amatta.amatta_server.user.model.Users;
import com.amatta.amatta_server.user.repository.UserRepository;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

@Service
public class UserService {

private final UserRepository userRepository;

private final DeviceTokenRepository tokenRepository;

private final MailService mailService;

public UserService(UserRepository userRepository, MailService mailService) {
@Autowired
public UserService(UserRepository userRepository, DeviceTokenRepository tokenRepository, MailService mailService) {
this.userRepository = userRepository;
this.tokenRepository = tokenRepository;
this.mailService = mailService;
}

Expand Down Expand Up @@ -49,6 +59,22 @@ public Users login(UserLoginReq userLoginReq) {
return null;
}

public void logout() {
HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
HttpSession session = request.getSession(false);

if(session == null) {
return;
}

Users user = (Users) session.getAttribute("User");

if(user == null) {
return;
}
tokenRepository.deleteTokenByUid(user.getId());
}

public UserFindEmailRes findEmail(UserFindEmailReq userFindEmailReq) {
Users users = userRepository.findByNameAndPhoneNum(userFindEmailReq.getName(), userFindEmailReq.getPhoneNumber());
if (Objects.nonNull(users)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ void token_by_expiration_date_test() {
LocalDate now = LocalDate.now();
userRepository.addUser("[email protected]", "testest1234", "테스트", "010-1111-1111");
long id = userRepository.last_insert_id();
gifticonRepository.addGifticon(id, "test".getBytes(), "test".getBytes(), "test brand", "test item", "12341234",
gifticonRepository.addGifticon(id, "test", "test", "test brand", "test item", "12341234",
now.plusDays(2), now.plusYears(100), 2000);
tokenRepository.addToken(id, "testtoken");
List<String> list = tokenRepository.findTokensByUidsOfGifticonsAboutToExpire(now.plusDays(Gifticon.expirationThresholdDays));
assertNotEquals(0, list.size());
}
}
}
Loading

0 comments on commit 1e29cc6

Please sign in to comment.