| 
 | 1 | +package controllers;  | 
 | 2 | + | 
 | 3 | +import models.User;  | 
 | 4 | +import org.apache.commons.codec.digest.DigestUtils;  | 
 | 5 | +import play.libs.Crypto;  | 
 | 6 | +import play.mvc.Security;  | 
 | 7 | +import services.UserDao;  | 
 | 8 | +import org.apache.commons.codec.binary.Base64;  | 
 | 9 | +import org.mindrot.jbcrypt.BCrypt;  | 
 | 10 | +import play.libs.Json;  | 
 | 11 | +import play.mvc.Controller;  | 
 | 12 | +import play.mvc.Result;  | 
 | 13 | + | 
 | 14 | +import javax.inject.Inject;  | 
 | 15 | +import javax.inject.Singleton;  | 
 | 16 | +import java.util.ArrayList;  | 
 | 17 | +import java.util.List;  | 
 | 18 | + | 
 | 19 | +@Singleton  | 
 | 20 | +public class UserController extends Controller {  | 
 | 21 | +    private final UserDao userDao;  | 
 | 22 | + | 
 | 23 | +    @Inject  | 
 | 24 | +    public UserController(UserDao userDao) {  | 
 | 25 | +        this.userDao = userDao;  | 
 | 26 | +    }  | 
 | 27 | + | 
 | 28 | +    public Result getUser(String id) {  | 
 | 29 | +        User user = userDao.get(id);  | 
 | 30 | +        if (user == null) {  | 
 | 31 | +            return notFound();  | 
 | 32 | +        }  | 
 | 33 | +        List<User> following = userDao.get(user.getFollowing());  | 
 | 34 | +        List<User> followers = userDao.get(user.getFollowers());  | 
 | 35 | +        UserView view = UserView.fromUser(user);  | 
 | 36 | +        view.followers = UserView.fromUsers(followers);  | 
 | 37 | +        view.following = UserView.fromUsers(following);  | 
 | 38 | +        return ok(Json.toJson(view));  | 
 | 39 | +    }  | 
 | 40 | + | 
 | 41 | +    public Result newUser() {  | 
 | 42 | +        UserSignup signup = Json.fromJson(request().body().asJson(), UserSignup.class);  | 
 | 43 | +        if (userDao.findByEmail(signup.email) != null) {  | 
 | 44 | +            return badRequest();  | 
 | 45 | +        }  | 
 | 46 | +        User user = new User(signup.email, signup.name, BCrypt.hashpw(signup.password, BCrypt.gensalt()));  | 
 | 47 | +        User newUser = userDao.create(user);  | 
 | 48 | +        session().put("username", newUser.getId());  | 
 | 49 | +        response().setHeader(LOCATION, routes.UserController.getUser(newUser.getId()).url());  | 
 | 50 | +        return created();  | 
 | 51 | +    }  | 
 | 52 | + | 
 | 53 | +    public Result signIn() {  | 
 | 54 | +        UserSignup signup = Json.fromJson(request().body().asJson(), UserSignup.class);  | 
 | 55 | +        User user = userDao.findByEmail(signup.email);  | 
 | 56 | +        if (user == null) {  | 
 | 57 | +            return notFound();  | 
 | 58 | +        }  | 
 | 59 | +        if (BCrypt.checkpw(signup.password, user.getPasswordHash())) {  | 
 | 60 | +            session().put("username", user.getId());  | 
 | 61 | +            return getUser(user.getId());  | 
 | 62 | +        } else {  | 
 | 63 | +            return notFound();  | 
 | 64 | +        }  | 
 | 65 | +    }  | 
 | 66 | + | 
 | 67 | +    @Security.Authenticated  | 
 | 68 | +    public Result current() {  | 
 | 69 | +        return getUser(request().username());  | 
 | 70 | +    }  | 
 | 71 | + | 
 | 72 | +    public Result search(String term) {  | 
 | 73 | +        return ok(Json.toJson(UserView.fromUsers(userDao.search(term))));  | 
 | 74 | +    }  | 
 | 75 | + | 
 | 76 | +    public Result logOut() {  | 
 | 77 | +        session().clear();  | 
 | 78 | +        return ok();  | 
 | 79 | +    }  | 
 | 80 | + | 
 | 81 | +    @Security.Authenticated  | 
 | 82 | +    public Result followUser(String id) {  | 
 | 83 | +        if (userDao.get(id) == null) {  | 
 | 84 | +            return notFound();  | 
 | 85 | +        }  | 
 | 86 | +        userDao.follow(request().username(), id);  | 
 | 87 | +        return ok();  | 
 | 88 | +    }  | 
 | 89 | + | 
 | 90 | +    @Security.Authenticated  | 
 | 91 | +    public Result unfollowUser(String id) {  | 
 | 92 | +        userDao.unfollow(request().username(), id);  | 
 | 93 | +        return ok();  | 
 | 94 | +    }  | 
 | 95 | + | 
 | 96 | +    public static class UserSignup {  | 
 | 97 | +        public String name;  | 
 | 98 | +        public String email;  | 
 | 99 | +        public String password;  | 
 | 100 | +    }  | 
 | 101 | + | 
 | 102 | +    public static class UserView {  | 
 | 103 | +        public String id;  | 
 | 104 | +        public String name;  | 
 | 105 | +        public String gravatar;  | 
 | 106 | +        public int updates;  | 
 | 107 | +        public List<UserView> followers;  | 
 | 108 | +        public List<UserView> following;  | 
 | 109 | + | 
 | 110 | +        static List<UserView> fromUsers(List<User> users) {  | 
 | 111 | +            List<UserView> views = new ArrayList<UserView>(users.size());  | 
 | 112 | +            for (User user: users) {  | 
 | 113 | +                views.add(fromUser(user));  | 
 | 114 | +            }  | 
 | 115 | +            return views;  | 
 | 116 | +        }  | 
 | 117 | + | 
 | 118 | +        static UserView fromUser(User user) {  | 
 | 119 | +            UserView view = new UserView();  | 
 | 120 | +            view.id = user.getId();  | 
 | 121 | +            view.name = user.getName();  | 
 | 122 | +            view.gravatar = "//www.gravatar.com/avatar/" + DigestUtils.md5Hex(user.getEmail());  | 
 | 123 | +            view.updates = user.getUpdates();  | 
 | 124 | +            return view;  | 
 | 125 | +        }  | 
 | 126 | +    }  | 
 | 127 | + | 
 | 128 | +}  | 
 | 129 | + | 
0 commit comments