Completed
Pull Request — dev (#306)
by Konstantin
03:50
created

show(Integer)   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 7
rs 10
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.IdentifiedModelException;
4
import easytests.api.v1.exceptions.NotFoundException;
5
import easytests.api.v1.exceptions.UnidentifiedModelException;
6
import easytests.api.v1.mappers.ObjectsMapper;
7
import easytests.api.v1.models.Identity;
8
import easytests.api.v1.models.Object;
9
import easytests.core.models.UserModel;
10
import easytests.core.models.UserModelInterface;
11
import easytests.core.options.SubjectsOptions;
12
import easytests.core.options.UsersOptions;
13
import easytests.core.options.UsersOptionsInterface;
14
import easytests.core.options.builder.UsersOptionsBuilder;
15
import easytests.core.services.UsersService;
16
import java.util.List;
17
import java.util.stream.Collectors;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.beans.factory.annotation.Qualifier;
20
import org.springframework.http.HttpStatus;
21
import org.springframework.web.bind.annotation.*;
22
23
24
/**
25
 * @author malinink
26
 */
27
@RestController("ObjectsControllerV1")
28
@SuppressWarnings("checkstyle:MultipleStringLiterals")
29
@RequestMapping("/v1/objects")
30
public class ObjectsController {
31
32
    @Autowired
33
    protected UsersService usersService;
34
35
    @Autowired
36
    private UsersOptionsBuilder usersOptionsBuilder;
37
38
    @Autowired
39
    @Qualifier("ObjectsMapperV1")
40
    private ObjectsMapper objectsMapper;
41
42
    /**
43
     * TODO
44
     * Add permissions check
45
     */
46
47
    @GetMapping("")
48
    public List<Object> list() {
49
        final List<UserModelInterface> usersModels = this.usersService.findAll();
50
51
        return usersModels
52
            .stream()
53
            .map(model -> this.objectsMapper.map(model, Object.class))
54
            .collect(Collectors.toList());
55
    }
56
57
    @PostMapping("")
58
    @ResponseStatus(HttpStatus.CREATED)
59
    public Identity create(@RequestBody Object object) throws Exception {
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
60
        if (object.getId() != null) {
61
            throw new IdentifiedModelException();
62
        }
63
64
        /**
65
         * We need to check for email existence in usersService
66
         * TODO
67
         */
68
69
        final UserModelInterface userModel = this.objectsMapper.map(object, UserModel.class);
70
71
        /**
72
         * Temporary set password cause it must be not null
73
         */
74
        userModel.setPassword("");
75
76
        this.usersService.save(userModel);
77
78
        return this.objectsMapper.map(userModel, Identity.class);
79
    }
80
81
    @PutMapping("")
82
    public void update(@RequestBody Object object) throws Exception {
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
83
        if (object.getId() == null) {
84
            throw new UnidentifiedModelException();
85
        }
86
87
        /**
88
         * We need to check for email existence in usersService
89
         * TODO
90
         */
91
92
        final UserModelInterface userModel = this.getUserModel(object.getId());
93
94
        this.objectsMapper.map(object, userModel);
95
96
        this.usersService.save(userModel);
97
    }
98
99
    @GetMapping("/{userId}")
100
    public Object show(@PathVariable Integer userId) {
101
        final UserModelInterface userModel = this.getUserModel(
102
            userId,
103
            (new UsersOptions()).withSubjects(new SubjectsOptions())
104
        );
105
        return this.objectsMapper.map(userModel, Object.class);
106
    }
107
108
    private UserModelInterface getUserModel(Integer id, UsersOptionsInterface userOptions) {
109
        final UserModelInterface userModel = this.usersService.find(id, userOptions);
110
        if (userModel == null) {
111
            throw new NotFoundException();
112
        }
113
        return userModel;
114
    }
115
116
    private UserModelInterface getUserModel(Integer id) {
117
        return this.getUserModel(id, this.usersOptionsBuilder.forAuth());
118
    }
119
}
120