Last active
March 19, 2019 03:51
-
-
Save hw0k/68cfe836eb5525349d3599d067843ecd to your computer and use it in GitHub Desktop.
Spring CRUD with no database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kr.hs.dgsw.user; | |
public class User { | |
private int id; | |
private String name; | |
private String email; | |
public User(int id, String name, String email) { | |
this.id = id; | |
this.name = name; | |
this.email = email; | |
} | |
public User() { | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kr.hs.dgsw.user; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.*; | |
import java.util.List; | |
@RestController | |
public class UserController { | |
@Autowired | |
private UserService userService; | |
@GetMapping("/user") | |
public @ResponseBody List<User> getAll() { | |
return userService.getAll(); | |
} | |
@GetMapping("/user/{id}") | |
public @ResponseBody User getOne(@PathVariable String id) { | |
return userService.getOne(Integer.parseInt(id)); | |
} | |
@PostMapping(value = "/user", consumes = "application/json") | |
public boolean add(@RequestBody User user) throws Exception { | |
return userService.add(user); | |
} | |
@PutMapping(value = "/user", consumes = "application/json") | |
public boolean update(@RequestBody User user) throws Exception { | |
return userService.update(user); | |
} | |
@DeleteMapping("/user/{id}") | |
public boolean delete(@PathVariable String id) { | |
return userService.delete(Integer.parseInt(id)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kr.hs.dgsw.user; | |
import java.util.List; | |
public interface UserService { | |
List<User> getAll(); | |
User getOne(int id); | |
boolean add(User user); | |
boolean update(User user); | |
boolean delete(int id); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kr.hs.dgsw.user; | |
import org.springframework.stereotype.Service; | |
import java.util.ArrayList; | |
import java.util.List; | |
@Service | |
public class UserServiceImpl implements UserService { | |
List<User> users; | |
public UserServiceImpl() { | |
users = new ArrayList<>(); | |
this.setupUsers(); | |
} | |
private void setupUsers() { | |
for (int i = 1; i <= 5; i++) { | |
users.add(new User(i, "user" + i, "user" + i + "@aa.co.kr")); | |
} | |
} | |
private User findOne(int id) { | |
for (User u: users) { | |
if (u.getId() == id) return u; | |
} | |
return null; | |
} | |
@Override | |
public List<User> getAll() { | |
return users; | |
} | |
@Override | |
public User getOne(int id) { | |
return this.findOne(id); | |
} | |
@Override | |
public boolean add(User user) { | |
if (this.findOne(user.getId()) != null) return false; | |
users.add(user); | |
return true; | |
} | |
@Override | |
public boolean update(User user) { | |
User updateUser = this.findOne(user.getId()); | |
if (updateUser == null) return false; | |
updateUser.setName(user.getName()); | |
updateUser.setEmail(user.getEmail()); | |
return true; | |
} | |
@Override | |
public boolean delete(int id) { | |
User user = this.findOne(id); | |
if (user == null) return false; | |
users.remove(user); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment