aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java
blob: 58b62df2a95e77df546baf14a739201ae23e9dba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.ufund.api.ufundapi.model;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;

public class User {

    public enum UserType {
        HELPER,
        MANAGER
    }

    @JsonProperty("username")
    private final String username;
    @JsonProperty("passwordHash")
    private int passwordHash;
    @JsonProperty("basket")
    private final List<Integer> basket;
    @JsonProperty("type")
    private final UserType type;

    /**
     * Create a new user
     * 
     * @param username The name of the user
     * @param basket   A basket to copy from
     */
    public User(@JsonProperty("username") String username, @JsonProperty("passwordHash") int passwordHash,
            @JsonProperty("basket") List<Integer> basket, @JsonProperty("type") UserType userType) {
        this.username = username;
        this.basket = basket;
        this.passwordHash = passwordHash;
        this.type = userType;
    }

    public static User create(String username, String password) {
        return new User(
                username,
                password.hashCode(),
                new ArrayList<>(),
                UserType.HELPER);
    }

    public String getUsername() {
        return username;
    }

    /**
     * Verifies if the provided password's hash is the same as the user's actual
     * hash
     * 
     * @param password The password to check if valid
     * @return True or false depending on if it's equal
     */
    public boolean verifyPassword(String password) {
        return password.hashCode() == passwordHash;
    }

    /**
     * Adds a need's ID to a user's basket
     * 
     * @param need The need to add
     */
    public void addToBasket(Need need) {
        basket.add(need.getId());
    }

    public Integer[] getBasket() {
        return basket.toArray(Integer[]::new);
    }

    public void removeBasketNeed(Integer needID) {
        basket.remove(needID);
    }

    /**
     * Returns a user without a password hash for security purposes
     * 
     * @return new User with empty password hash
     */
    public User withoutPasswordHash() {
        return new User(this.username, 0, this.basket, this.type);
    }

    public UserType getType() {
        return type;
    }

    public void copyPassword(User other) {
        this.passwordHash = other.passwordHash;
    }

    @Override
    public String toString() {
        return this.username + "; basket: " + this.basket + "; type:" + this.type + "; hash: " + this.passwordHash;
    }

}