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

update(Object)   A

Complexity

Conditions 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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