Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1 (#35) #36

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'java'
id 'org.springframework.boot' version '2.7.8'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
id 'war'
}

group = 'com.greenpoint'
Expand All @@ -14,13 +15,20 @@ configurations {
}
}

bootWar {
enabled = true
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.3'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.3'
implementation 'org.springframework.boot:spring-boot-starter'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/greenpoint/server/ServerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@SpringBootApplication
public class ServerApplication {
@EnableJpaAuditing
public class ServerApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ServerApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}

}



Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.greenpoint.server.admin.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@CrossOrigin
@RequestMapping("/api")
public class AdminController {

}
34 changes: 34 additions & 0 deletions src/main/java/com/greenpoint/server/admin/model/Admin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.greenpoint.server.admin.model;


import com.greenpoint.server.common.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Where(clause = "deleted = false")
@SQLDelete(sql = "UPDATE admin SET deleted = true Where id = ?")
public class Admin extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String adminID;
private String password;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.greenpoint.server.admin.repository;


import com.greenpoint.server.admin.model.Admin;
import org.springframework.data.jpa.repository.JpaRepository;

public interface AdminRepository extends JpaRepository<Admin, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.greenpoint.server.admin.service;

import org.springframework.stereotype.Service;

@Service
public class AdminService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.greenpoint.server.auth.controller;

import com.greenpoint.server.auth.response.AccountResponse;
import com.greenpoint.server.auth.response.LoginResponse;
import com.greenpoint.server.auth.service.UserService;
import com.greenpoint.server.customer.model.Customer;
import com.greenpoint.server.customer.repository.CustomerRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequiredArgsConstructor
@CrossOrigin
@RequestMapping("/api")
public class UserController {
@Autowired
private UserService userService;

@GetMapping("/login")
public ResponseEntity<LoginResponse> getLogin(@RequestParam("code") String code) {

boolean isRegistered;
Object[] obArr = new Object[2];
Customer userInfo = new Customer();
obArr = userService.saveUser(code);
System.out.println("obArr = " + obArr);
userInfo = (Customer) obArr[0];
isRegistered = (boolean) obArr[1];
LoginResponse loginResponse = new LoginResponse(userInfo.getKakaoId(), userInfo.getKakaoToken(), userInfo.getNickname(), userInfo.getImage(), isRegistered);
return ResponseEntity.ok(loginResponse);
}

@PostMapping("/makeAccount")
public ResponseEntity<AccountResponse> makeAccount(@RequestBody HashMap<String, String> param) {
Boolean result;
System.out.println("param = " + param);
result = userService.getUserInfoByForm(param);
if(result == false) {
AccountResponse accountResponse = new AccountResponse(false);
return ResponseEntity.ok(accountResponse);
}
AccountResponse accountResponse = new AccountResponse(true);
return ResponseEntity.ok(accountResponse);
}

@Transactional
@DeleteMapping("/deleteMember")
public String deleteAccount(@RequestParam("id") Long id){
String resultCode = "";
int result = userService.memberDelete(id);
if(result == 0){
resultCode = "회원 탈퇴 실패";

} else {
// session.removeAttribute("sessionId");
resultCode = "회원 탈퇴 성공";
}
return resultCode;
}

}

19 changes: 19 additions & 0 deletions src/main/java/com/greenpoint/server/auth/model/KakaoProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.greenpoint.server.auth.model;

import lombok.Data;

@Data
public class KakaoProfile {
private Long id;
private Properties properties;



@Data
public class Properties {
public String nickname;
public String profile_image; // 이미지 경로 필드1
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
//package com.greenpoint.server.auth.repository;
//
//import com.greenpoint.server.customer.model.Customer;
//import org.springframework.data.jpa.repository.JpaRepository;
//
//public interface UserRepository extends JpaRepository<Customer, Long> {
//
//
//}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.greenpoint.server.auth.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class AccountResponse {
// private String nickname;
// private String contact;
private boolean isRegistered;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.greenpoint.server.auth.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class LoginResponse {
private Long kakaoId;
private String token;
private String nickname;
private String image;
private boolean isRegistered;
}
118 changes: 118 additions & 0 deletions src/main/java/com/greenpoint/server/auth/service/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.greenpoint.server.auth.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.greenpoint.server.auth.model.KakaoProfile;
import com.greenpoint.server.auth.model.KakaoProfile;
import com.greenpoint.server.customer.model.Customer;
import com.greenpoint.server.customer.repository.CustomerRepository;
import com.greenpoint.server.level.service.LevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.sql.SQLOutput;

import javax.servlet.http.HttpSession;
import java.sql.SQLOutput;
import java.util.HashMap;

@Service
public class UserService {
@Autowired
private CustomerRepository customerRepository;
@Autowired
private LevelService levelService;
public Object[] saveUser(String token) {

Object[] obArr = new Object[2];
String name = null;
String imageUrl = null;
Long id = null;

String userInfo = getUserInfoByAccessToken(token);
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode jsonNode = objectMapper.readTree(userInfo);
System.out.println(jsonNode);
id = Long.valueOf(String.valueOf(jsonNode.get("id")));
System.out.println("id = " + id);
name = String.valueOf(jsonNode.get("kakao_account").get("profile").get("nickname"));
imageUrl = String.valueOf(jsonNode.get("kakao_account").get("profile").get("profile_image_url"));
} catch (JsonProcessingException e) {
e.printStackTrace();
}

System.out.println("userInfo" + userInfo);

Customer user;

user = customerRepository.findByKakaoId(id);
System.out.println("user = " + user);
if(user == null) {
Customer newUser = Customer.from(id, token, name.substring(1, name.length() - 1), imageUrl.substring(1, imageUrl.length() - 1));
obArr[0] = newUser;
obArr[1] = false;
customerRepository.save(newUser);
return obArr;
}

obArr[0] = user;
obArr[1] = true;
return obArr;
}


public String getUserInfoByAccessToken(String accessToken) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + accessToken);
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);

String url = "https://kapi.kakao.com/v2/user/me";

return restTemplate.postForObject(url, request, String.class);
}
public Boolean getUserInfoByForm(HashMap<String, String> param) {
Customer user;

Long id = Long.valueOf(param.get("id"));
System.out.println("id = " + id);
try{
user = customerRepository.findByKakaoId(id);
user.setContact(param.get("contact"));
user.setNickname(param.get("nickname"));
user.setLatitude(Double.valueOf(param.get("latitude")));
user.setLongitude(Double.valueOf(param.get("longitude")));
user.setPoint(0);
user.setTotalPoint(0);
user.setLevel(levelService.findByGrade(1));
// user = Customer.from(param.get("nickname"), param.get("contact"));
user = customerRepository.save(user);
if(user == null){
return false;
}
}
catch(Exception e){
System.out.println("토큰이 다름!");
return false;
}

return true;
}


public int memberDelete(Long id) {
int result = customerRepository.deleteByKakaoId(id);
return result;
}

}
Loading