Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

회원 기본 API #39

Merged
merged 2 commits into from
Oct 3, 2016
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
73 changes: 73 additions & 0 deletions src/main/java/io/codechobo/member/application/MemberService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.codechobo.member.application;

import io.codechobo.member.domain.Member;
import io.codechobo.member.domain.PointPerLevel;
import io.codechobo.member.domain.repository.MemberRepository;
import io.codechobo.member.domain.support.MemberDto;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.NotNull;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author loustler
* @since 10/02/2016 10:12
*/
public class MemberService {
@Autowired
private MemberRepository memberRepository;

public List<MemberDto> getMembers() {
List<Member> member = memberRepository.findAll();

return member.stream()
.map(m -> new MemberDto(m.getSeq(), m.getId(), m.getPassword(), m.getEmail(), m.getNickName(), m.getRegistrationDate(), m.getPoint()))
.collect(Collectors.toList());
}

public MemberDto getMember(final Long sequence) {
Member member = memberRepository.findOne(sequence);

if(member == null) return null;

return new MemberDto(member.getSeq(), member.getId(), member.getPassword(), member.getEmail(), member.getNickName(), member.getRegistrationDate(), member.getPoint());
}

public MemberDto createMember(final MemberDto memberDto) {
Member member = new Member(memberDto);

if(member.getPoint() == null)
member.setPoint(new Integer(0));

if(member.getRegistrationDate() == null)
member.setRegistrationDate(Calendar.getInstance().getTime());

member.setLevel(PointPerLevel.valueOf(member.getPoint()));

member = memberRepository.save(member);

return new MemberDto(member.getSeq(), member.getId(), member.getPassword(), member.getEmail(), member.getNickName(), member.getRegistrationDate(), member.getPoint());
}

public MemberDto updateMember(MemberDto memberDto) {
if(memberDto.getSequence() == null || !memberRepository.exists(memberDto.getSequence())) {
throw new IllegalArgumentException();
}

memberDto.setLevel(PointPerLevel.valueOf(memberDto.getPoint()));

Member member = memberRepository.save(new Member(memberDto));

return new MemberDto(member.getSeq(), member.getId(), member.getPassword(), member.getEmail(), member.getNickName(), member.getRegistrationDate(), member.getPoint());
}

public void deleteMember(@NotNull final Long memberSequence) {
if(memberSequence == null) {
throw new IllegalArgumentException();
}

memberRepository.delete(memberSequence);
}
}
45 changes: 11 additions & 34 deletions src/main/java/io/codechobo/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.codechobo.member.domain;


import io.codechobo.member.domain.support.MemberDto;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
Expand All @@ -14,12 +14,9 @@
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

Expand Down Expand Up @@ -68,38 +65,18 @@ public class Member {
/*
* social은 member에 종속적이므로 cascade
*/
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "SOCIAL_SEQ")
@OneToMany(fetch = FetchType.EAGER, mappedBy = "seq")
private List<Social> socials;

public Member(String id, String password, String nickName, String email, Integer point, ArrayList<Social> socials) {
this.id = id;
this.password = password;
this.nickName = nickName;
this.email = email;
this.point = point;
this.level = PointPerLevel.valueOf(this.point);
this.socials = socials;
this.registrationDate = new Date();
public Member(final MemberDto memberDto) {
this.seq = memberDto.getSequence();
this.id = memberDto.getId();
this.password = memberDto.getPassword();
this.nickName = memberDto.getNickName();
this.email = memberDto.getEmail();
this.point = memberDto.getPoint();
this.level = memberDto.getLevel();
this.registrationDate = memberDto.getRegiDate();
}

@PrePersist
public void tmpLevelPersist() {
this.level = PointPerLevel.valueOf(this.point);
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder("[");
builder.append("sequence : "+this.seq)
.append(", id : "+this.id)
.append(", password : "+this.password)
.append(", nickName : "+this.nickName)
.append(", email : "+this.email)
.append(", point : "+this.point)
.append(", level : "+this.level)
.append("]");

return builder.toString();
}
}
3 changes: 0 additions & 3 deletions src/main/java/io/codechobo/member/domain/PointPerLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ public enum PointPerLevel {
this.max = max;
}

public int getMin() {return this.min;}
public int getMax() {return this.max;}

public static PointPerLevel valueOf(int point) {
if(0 <= point && point < 100) return BASIC;
else if(100 <= point && point < 200) return STANDARD;
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/io/codechobo/member/domain/Social.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.codechobo.member.domain;


import io.codechobo.member.domain.support.SocialDto;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand Down Expand Up @@ -38,15 +39,9 @@ public class Social {
@JoinColumn(name = "MEMBER_SEQ")
private Member member;

@Override
public String toString() {
StringBuilder builder = new StringBuilder("[");
builder.append("seq : "+this.seq)
.append(", type : "+this.type)
.append(", token : "+this.token)
.append(", member : "+this.member)
.append("]");

return builder.toString();
public Social(final SocialDto socialDto) {
this.seq = socialDto.getSequence();
this.type = socialDto.getType();
this.token = socialDto.getToken();
}
}
49 changes: 49 additions & 0 deletions src/main/java/io/codechobo/member/domain/support/MemberDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.codechobo.member.domain.support;

import io.codechobo.member.domain.PointPerLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Date;

/**
* @author loustler
* @since 10/02/2016 10:13
*/
@Getter
@Setter
@NoArgsConstructor
public class MemberDto {
private Long sequence;
private String id;
private String password;
private String email;
private String nickName;
private Date regiDate;
private Integer point;
private PointPerLevel level;

public MemberDto(final Long sequence) {
this.sequence = sequence;
}

public MemberDto(final String id, final String password, final String email, final String nickName) {
this(null, id, password, email, nickName, null, 0);
}

public MemberDto(final String id, final String password, final String email, final String nickName, final Date regiDate, final Integer point) {
this(null, id, password, email, nickName, regiDate, point);
}

public MemberDto(final Long sequence, final String id, final String password, final String email, final String nickName, final Date regiDate, final Integer point) {
this.sequence = sequence;
this.id = id;
this.password = password;
this.email = email;
this.nickName = nickName;
this.regiDate = regiDate;
this.point = point;
this.level = PointPerLevel.valueOf(this.point);
}
}
34 changes: 34 additions & 0 deletions src/main/java/io/codechobo/member/domain/support/SocialDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.codechobo.member.domain.support;

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

/**
* @author loustler
* @since 10/02/2016 10:15
*/
@Getter
@Setter
@NoArgsConstructor
public class SocialDto {
private Long sequence;
private String type;
private String token;
private Long memberSequence;

public SocialDto(final Long sequence) {
this.sequence = sequence;
}

public SocialDto(final String type, final String token, final Long memberSequence) {
this(null, type, token, memberSequence);
}

public SocialDto(final Long sequence, final String type, final String token, final Long memberSequence) {
this.sequence = sequence;
this.type = type;
this.token = token;
this.memberSequence = memberSequence;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.codechobo.member.interfaces.api;

import io.codechobo.member.application.MemberService;
import io.codechobo.member.domain.support.MemberDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
* @author loustler
* @since 10/02/2016 10:17
*/
@RestController
@RequestMapping("/api/member")
public class MemberRestController {
@Autowired
MemberService memberService;

@RequestMapping(value = {"","/"}, method = RequestMethod.POST)
public ResponseEntity save(@RequestBody MemberDto memberDto) {
return new ResponseEntity(memberService.createMember(memberDto), HttpStatus.CREATED);
}

@RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
public ResponseEntity show() {
return new ResponseEntity(memberService.getMembers(), HttpStatus.OK);
}

@RequestMapping(value = "/{seq}", method = RequestMethod.GET)
public ResponseEntity show(@PathVariable Long seq) {
return new ResponseEntity(memberService.getMember(seq), HttpStatus.OK);
}

@RequestMapping(value = "/{seq}", method = RequestMethod.PUT)
public ResponseEntity update(@PathVariable Long seq, @RequestBody MemberDto memberDto) {
memberDto.setSequence(seq);
return new ResponseEntity(memberService.updateMember(memberDto), HttpStatus.OK);
}

@RequestMapping(value = "/{seq}", method = RequestMethod.DELETE)
public ResponseEntity delete(@PathVariable Long seq) {
memberService.deleteMember(seq);
return new ResponseEntity(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.codechobo.member.application;

import io.codechobo.member.domain.PointPerLevel;
import io.codechobo.member.domain.support.MemberDto;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

/**
* @author loustler
* @since 10/02/2016 10:19
*/
public class MemberServiceIntegrationTest {
@Autowired
MemberService memberService;

@Test
public void 멤버등록_테스트_crate_member() {
final MemberDto dto = generateData();
final MemberDto member = memberService.createMember(dto);

assertNotNull(member);
assertThat(member.getNickName(), is("nickName"));
System.out.println(member.getLevel());
}

@Test
public void 멤버_업데이트_update_member() {
final MemberDto dto = generateData();
final MemberDto member = memberService.createMember(dto);

final MemberDto updateDto = new MemberDto(member.getSequence(), member.getId(), "another password", "[email protected]", "loustler", Calendar.getInstance().getTime(), new Integer(100));
memberService.updateMember(updateDto);

final MemberDto updateMember = memberService.getMember(member.getSequence());

assertNotNull(updateMember);
assertThat(updateMember.getNickName(), is("loustler"));
assertThat(updateMember.getLevel(), is(PointPerLevel.STANDARD));
}

@Test
public void 멤버_삭제_remove_member() {
final MemberDto dto = generateData();
final MemberDto member = memberService.createMember(dto);

memberService.deleteMember(member.getSequence());

MemberDto after = memberService.getMember(member.getSequence());

assertNull(after);
}

private MemberDto generateData() {
return new MemberDto("id", "password", "[email protected]", "nickName", Calendar.getInstance().getTime(), new Integer(0));
}
}
Loading