aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java
blob: def21c6a017657552bee177154087f9407617e0b (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.ufund.api.ufundapi.persistence;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ufund.api.ufundapi.model.User;

@Component
public class UserFileDAO implements UserDAO {

    private final Map<String, User> users; // cache
    private final ObjectMapper objectMapper;
    private final String filename;

    public UserFileDAO(@Value("${users.file}") String filename, ObjectMapper objectMapper) throws IOException {
        this.filename = filename;
        this.objectMapper = objectMapper;
        users = new HashMap<>();
        load(); // load the users from the file
    }

    /**
     * Load changes from the json file
     *
     * @throws IOException Any IO issue with the file
     */
    private void load() throws IOException {
        users.clear();

        User[] usersArray = objectMapper.readValue(new File(filename), User[].class);

        for (User user : usersArray) {
            users.put(user.getUsername(), user);
        }
    }

    /**
     * Saves the needs to json
     *
     * @return True if the save was successful, false otherwise
     * @throws IOException If there was an IO issue saving the file
     */
    private boolean save() throws IOException {
        objectMapper.writeValue(new File(filename), users.values());
        return true;
    }

    @Override
    public User[] getUsers() {
        synchronized (users) {
            return users.values().toArray(User[]::new);
        }
    }

    @Override
    public User getUser(String username) {
        synchronized (users) {
            return users.getOrDefault(username, null);
        }
    }

    @Override
    public User addUser(User user) throws IOException {
        synchronized (users) {
            var res = users.putIfAbsent(user.getUsername(), user);
            save();
            if (res == null) {
                return user;
            }
            return res;
        }
    }

    @Override
    public User updateUser(User user) throws IOException {
        synchronized (users) {
            if (users.containsKey(user.getUsername())) {
                // var old = users.put(user.getUsername(), user);
                // user.copyPassword(old);
                if (user.getBasketNeeds() == null || user.getType() == null) {
                    User oldData = users.get(user.getUsername());
                    User crutch = new User(oldData.getUsername(), 0, new ArrayList<Integer>(), oldData.getType());
                    crutch.copyPassword(oldData);
                    users.put(user.getUsername(), crutch);
                } else {
                    var old = users.put(user.getUsername(), user);
                    user.copyPassword(old);
                }
                save();
                return user;
            } else {
                return null;
            }
        }
    }

    @Override
    public boolean deleteUser(String username) throws IOException {
        synchronized (users) {
            if (users.containsKey(username)) {
                users.remove(username);
                return save();
            } else {
                return false;
            }
        }
    }

}