Fix the branch and add a test

This commit is contained in:
cugurlu
2019-03-17 18:58:56 +01:00
parent 0c042daa1c
commit 1da7f62c95
9 changed files with 104 additions and 213 deletions

View File

@@ -3,15 +3,17 @@ package greenify.server.data.model;
import lombok.Data;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import java.util.Objects;
@EnableAutoConfiguration
@Entity
@Data
@Table(name = "users")
public class User {
@Id
@@ -74,7 +76,9 @@ public class User {
return password;
}
public void setPassword(String password) { this.password = password; }
public void setPassword(String password) {
this.password = password;
}
/**
* gets the number of vegan meal.
@@ -84,58 +88,7 @@ public class User {
return veganMeal;
}
public void setVeganMeal(int veganMeal) { this.veganMeal = veganMeal; }
/**
* checks if two users are equal.
* @param other the object to compare the user with
* @return a boolean value of true if the user is equal to the object
*/
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null) {
return false;
}
if (getClass() != other.getClass()) {
return false;
}
if (other instanceof User) {
User that = (User) other;
return this.getName().equals(that.getName())
&& this.getId().equals(that.getId())
&& this.getPassword().equals(that.getPassword())
&& this.getVeganMeal() == that.getVeganMeal();
}
return false;
}
/**
* creates a string of the user object.
* in the form of: User(id=, name=, password=, veganMeal=)
* @return a string of the user object
*/
@Override
public String toString() {
return "User(id="
+ this.id
+ ", name="
+ this.name
+ ", password="
+ this.password
+ ", veganMeal="
+ this.veganMeal + ")";
}
/**
* hashes the User object.
* @return a hashcode for the user object
*/
@Override
public int hashCode() {
return Objects.hash(id, name, password, veganMeal);
public void setVeganMeal(int veganMeal) {
this.veganMeal = veganMeal;
}
}

View File

@@ -4,11 +4,8 @@ import greenify.server.data.model.User;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Integer> {
// User findByName(@Param("name") String name);
User findByName(String name);
User findById(Long id);
<T extends User> T save(T user);
}

View File

@@ -2,8 +2,8 @@ package greenify.server.rest;
import greenify.common.UserDto;
import greenify.server.data.model.User;
import greenify.server.service.UserService;
import greenify.server.data.repository.UserRepository;
import greenify.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -16,6 +16,10 @@ public class UserController {
@Autowired
UserService userService;
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
UserRepository userRepository;
@RequestMapping("/registerUser")
public UserDto registerUser(@RequestParam(value = "name") String name,
@RequestParam(value = "password") String password) {
@@ -34,11 +38,6 @@ public class UserController {
userService.addVeganMeal(id, name);
}
@GetMapping("/getUsername")
public void getUsername(@RequestParam(value = "id") Long id) {
userService.getUsername(id);
@GetMapping(path = "/all")
@ResponseBody
public Iterable<User> getAllUsers() {

View File

@@ -23,16 +23,15 @@ public class UserService {
*/
public UserDto registerUser(String name, String password) {
User user = userRepository.findByName(name);
if (user != null) {
throw new ApplicationException("User already exists");
} else {
if (user == null) {
user = userRepository.save(new User(null, name, password, 0));
} else {
throw new ApplicationException("User already exists");
}
logger.info("Created user id=" + user.getId() + ", name=" + user.getName());
return new UserDto(user.getId(), user.getName());
}
/**
* logs the user in.
* @param name the username of the user
@@ -61,20 +60,9 @@ public class UserService {
int count = user.getVeganMeal();
count++;
user.setVeganMeal(count);
logger.info("Added vegan meal to user(id="
+ user.getId() + ", name=" + user.getName() + ")");
}
/**
* gets the username of the user with the specified id.
* @param id the id of the user
* @return the username of the user
*/
public String getUsername(Long id) {
User user = userRepository.findById(id);
String name = user.getName();
logger.info("retrieved username from user with username=" + name + ", id=" + id);
return name;
userRepository.save(user);
logger.info("Added vegan meal to user(id=" + user.getId()
+ ", name=" + user.getName() + ")");
}
}