Skip to content
Open
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
61 changes: 31 additions & 30 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/.idea/
/target/
*.iml
/out/
/.classpath
/.project
/.settings

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/.idea/
/target/
*.iml
/out/
/.classpath
/.project
/.settings

/.externalToolBuilders/
48 changes: 29 additions & 19 deletions src/main/java/io/khasang/teamnote/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,25 @@
* @author MickeyMouse
*/
@Controller
@RequestMapping(value = "/users")
@RequestMapping(value = "/rest/users")
public class UserController {

@Autowired
private UserService userService;

@RequestMapping(value = "/", method = RequestMethod.PUT, produces = "application/json;charset=utf-8")
@RequestMapping(value = "/{userId}/addRole/{roleId}", method = RequestMethod.POST)
@ResponseBody
public User addUser(@RequestBody User user) {
return userService.addUser(user);
}

@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public List<User> getAllUsers() {
return userService.getAll();
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public User getById(@PathVariable(value = "id") String userIdStr) {
public User addRoletoUser(@PathVariable(value = "userId") String userIdStr,
@PathVariable(value = "roleId") String roleIdStr) {
long userId = Long.parseLong(userIdStr);
return userService.getById(userId);
long roleId = Long.parseLong(roleIdStr);
return userService.addRoletoUser(userId, roleId);
}

@RequestMapping(value = "/", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
@RequestMapping(method = RequestMethod.PUT, produces = "application/json;charset=utf-8")
@ResponseBody
public User update(@RequestBody User user) {
return userService.update(user);
public User addUser(@RequestBody User user) {
return userService.addUser(user);
}

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
Expand All @@ -57,6 +47,12 @@ public User delete(@PathVariable(value = "id") String userIdStr) {
return userService.delete(userId);
}

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public List<User> getAllUsers() {
return userService.getAll();
}

@RequestMapping(value = "/accountName={accountName}", method = RequestMethod.GET)
@ResponseBody
public User getByAccountName(@PathVariable(value = "accountName") String accountName) {
Expand All @@ -69,10 +65,24 @@ public User getByEmail(@PathVariable(value = "email") String email) {
return userService.getByEmail(email);
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public User getById(@PathVariable(value = "id") String userIdStr) {
long userId = Long.parseLong(userIdStr);
return userService.getById(userId);
}

@RequestMapping(value = "/firstName={firstName}&lastName={lastName}", method = RequestMethod.GET)
@ResponseBody
public List<User> getByPersonName(@PathVariable(value = "firstName") String firstName,
@PathVariable(value = "lastName") String lastName) {
return userService.getByPersonName(firstName, lastName);
}

@RequestMapping(method = RequestMethod.POST, produces = "application/json;charset=utf-8")
@ResponseBody
public User update(@RequestBody User user) {
return userService.update(user);
}

}
21 changes: 18 additions & 3 deletions src/main/java/io/khasang/teamnote/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package io.khasang.teamnote.entity;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

Expand Down Expand Up @@ -36,14 +40,17 @@ public class User {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private Long id;

@Column(name = "LAST_NAME")
private String lastName;

@Column(name = "PASSWORD")
private String password;

@ManyToMany(fetch = FetchType.EAGER)
private List<Role> roles;

public String getAccountName() {
return accountName;
}
Expand All @@ -56,7 +63,7 @@ public String getFirstName() {
return firstName;
}

public long getId() {
public Long getId() {
return id;
}

Expand All @@ -68,6 +75,10 @@ public String getPassword() {
return password;
}

public List<Role> getRoles() {
return roles;
}

public void setAccountName(String accountName) {
this.accountName = accountName;
}
Expand All @@ -80,7 +91,7 @@ public void setFirstName(String firstName) {
this.firstName = firstName;
}

public void setId(long id) {
public void setId(Long id) {
this.id = id;
}

Expand All @@ -92,4 +103,8 @@ public void setPassword(String password) {
this.password = password;
}

public void setRoles(List<Role> roles) {
this.roles = roles;
}

}
45 changes: 27 additions & 18 deletions src/main/java/io/khasang/teamnote/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
*/
public interface UserService {

/**
* Adds role to user.
*
* @param userId
* @param roleId
*/
User addRoletoUser(long userId, long roleId);

/**
* Adds user to database.
*
Expand All @@ -36,24 +44,6 @@ public interface UserService {
*/
List<User> getAll();

/**
* Finds user in database.
*
* @param id
* user ID
* @return user
*/
User getById(long id);

/**
* Updates specific user in database.
*
* @param user
* user to update
* @return updated user
*/
User update(User user);

/**
* Returns a {@link User} with a given account name or null if such user not found.
*
Expand All @@ -72,6 +62,15 @@ public interface UserService {
*/
User getByEmail(String email);

/**
* Finds user in database.
*
* @param id
* user ID
* @return user
*/
User getById(long id);

/**
* Returns a list of all {@link User}s with a given first and last names. Many users can have the same first and
* last name. That is why this method can return more than one {@link User}.
Expand All @@ -83,4 +82,14 @@ public interface UserService {
* @return users having given first and last name
*/
List<User> getByPersonName(String firstName, String lastName);

/**
* Updates specific user in database.
*
* @param user
* user to update
* @return updated user
*/
User update(User user);

}
32 changes: 22 additions & 10 deletions src/main/java/io/khasang/teamnote/service/impl/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.khasang.teamnote.dao.RoleDao;
import io.khasang.teamnote.dao.UserDao;
import io.khasang.teamnote.entity.Role;
import io.khasang.teamnote.entity.User;
import io.khasang.teamnote.service.UserService;

Expand All @@ -17,9 +19,20 @@
@Service("userService")
public class UserServiceImpl implements UserService {

@Autowired
private RoleDao roleDao;

@Autowired
private UserDao userDao;

public User addRoletoUser(long userId, long roleId) {
User user = userDao.getById(userId);
Role role = roleDao.getById(roleId);
user.getRoles().add(role);
userDao.update(user);
return user;
}

@Override
public User addUser(User user) {
return userDao.create(user);
Expand All @@ -36,16 +49,6 @@ public List<User> getAll() {
return userDao.getList();
}

@Override
public User getById(long id) {
return userDao.getById(id);
}

@Override
public User update(User user) {
return userDao.update(user);
}

@Override
public User getByAccountName(String accountName) {
return userDao.getByAccountName(accountName);
Expand All @@ -56,9 +59,18 @@ public User getByEmail(String email) {
return userDao.getByEmail(email);
}

@Override
public User getById(long id) {
return userDao.getById(id);
}

@Override
public List<User> getByPersonName(String firstName, String lastName) {
return userDao.getByPersonName(firstName, lastName);
}

@Override
public User update(User user) {
return userDao.update(user);
}
}
Loading