aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java
blob: f08f9f00774bba1b556c5382765b417eb06d01c2 (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
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;
    }

    public boolean verifyPassword(String password) {
        return password.hashCode() == passwordHash;
    }

    public void addToBasket(Need need) {
        basket.add(need.getId());
    }

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

    public void removeBasketNeed(Need need) {
        basket.remove(need.getId());
    }

    public User withoutPasswordHash() {
        return new User(this.username, 0, this.basket, this.type);
    }

    public UserType getType() {
        return type;
    }

}