프로젝트

8. 회원 정보 수정

uni5948 2021. 11. 23. 20:42

8. 회원 정보 수정

  • 서비스 추가
package com.example.service;

더보기
import com.example.entity.Member;
import org.springframework.stereotype.Service;
@Service
public interface MemberService {
   
    //회원가입
    public void insertUser(Member member);

    //회원 정보 찾기
    public Member selectUserOne(String id);

    //회원 정보 수정
    public void updateUser(Member member);
}
 
  • 서비스 생성
package com.example.service;

 

import java.util.Optional;

 

import javax.persistence.EntityManagerFactory;

 

import com.example.entity.Member;
import com.example.repository.MemberRepository;

 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

 

@Service
public class MemberServiceImpl implements MemberService{

 

    @Autowired
    EntityManagerFactory emf;

 

    @Autowired
    MemberRepository mRepository;

 

    //회원가입
    @Override
    public void insertUser(Member user) {
        mRepository.save(user);
    }

 

    //회원 정보 찾기
    @Override
    public Member selectUserOne(String id) {
        Optional<Member> member = mRepository.findById(id);
        return member.orElse(null);
    }

 

    //회원 정보 수정
    @Override
    public void updateUser(Member member) {
        mRepository.save(member);
    }
   
}

 

  • 컨트롤러 추가
더보기
package com.example.controller;
import java.util.HashMap;
import java.util.Map;
import com.example.entity.Member;
import com.example.jwt.JwtUtil;
import com.example.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MemberController {
    @Autowired
    MemberService mService;
    @Autowired
    JwtUtil jwtUtil;
    @Autowired
    AuthenticationManager authenticationManager;
    //회원가입
    //127.0.0.1:8080/REST/member/join
    @RequestMapping(value = "/member/join", method = {RequestMethod.POST},
    consumes = MediaType.ALL_VALUE,
    produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> joinPOST(@RequestBody Member member){
        Map<String, Object> map = new HashMap<String, Object>();
        try{
            BCryptPasswordEncoder bcpe = new BCryptPasswordEncoder();
            member.setUserpw(bcpe.encode(member.getUserpw()));
            mService.insertUser(member);
            map.put("status", 200);
        }
        catch(Exception e){
            e.printStackTrace();
            map.put("status", e.hashCode());
        }
        return map;
    }
    //로그인
    //127.0.0.1:8080/REST/member/login
    @RequestMapping(value = "/member/login", method = {RequestMethod.POST},
    consumes = MediaType.ALL_VALUE,
    produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> loginPOST(@RequestBody Member member) {
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            authenticationManager
                .authenticate(new UsernamePasswordAuthenticationToken(
                    member.getUserid(), member.getUserpw()));
            map.put("result", 1L);
            map.put("token", jwtUtil.generateToken(member.getUserid()));
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", e.hashCode());
        }
        return map;
    }

    //회원 정보 수정
    //127.0.0.1:8080/REST/member/update
    @RequestMapping(value = "/member/update", method = {RequestMethod.PUT},
    consumes = MediaType.ALL_VALUE,
    produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> updatePUT(@RequestBody Member member,
        @RequestHeader("token") String token) {
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            String userid = jwtUtil.extractUsername(token);
            Member member2 = mService.selectUserOne(userid);
            member2.setUserid(member.getUserid());
            member2.setUsername(member.getUsername());
            mService.updateUser(member2);
            map.put("result", 1L);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", e.hashCode());
        }
        return map;
    }
}
  • 회원 수정 구현

회원 수정 구현

  • db 확인

정보 수정 확인

'프로젝트' 카테고리의 다른 글

10. 선수 등록  (0) 2021.11.25
9. 팀, 에이전트 등록  (0) 2021.11.24
7. 로그인  (0) 2021.11.22
6. 회원가입  (0) 2021.11.18
5. db 연동  (0) 2021.11.18