Completed
Pull Request — dev (#306)
by Konstantin
04:39
created

update(Object)   A

Complexity

Conditions 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
c 2
b 0
f 0
dl 0
loc 19
rs 9.45
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.BadRequestException;
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
        if (object.getSubjects() != null) {
65
            throw new BadRequestException("subjects must be absent");
66
        }
67
68
        /**
69
         * We need to check for email existence in usersService
70
         * TODO
71
         */
72
73
        final UserModelInterface userModel = this.objectsMapper.map(object, UserModel.class);
74
75
        /**
76
         * Temporary set password cause it must be not null
77
         */
78
        userModel.setPassword("");
79
80
        this.usersService.save(userModel);
81
82
        return this.objectsMapper.map(userModel, Identity.class);
83
    }
84
85
    @PutMapping("")
86
    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...
87
        if (object.getId() == null) {
88
            throw new UnidentifiedModelException();
89
        }
90
        if (object.getSubjects() != null) {
91
            throw new BadRequestException("subjects must be absent");
92
        }
93
94
        /**
95
         * We need to check for email existence in usersService
96
         * TODO
97
         */
98
99
        final UserModelInterface userModel = this.getUserModel(object.getId());
100
101
        this.objectsMapper.map(object, userModel);
102
103
        this.usersService.save(userModel);
104
    }
105
106
    @GetMapping("/{userId}")
107
    public Object show(@PathVariable Integer userId) {
108
        final UserModelInterface userModel = this.getUserModel(
109
            userId,
110
            (new UsersOptions()).withSubjects(new SubjectsOptions())
111
        );
112
        return this.objectsMapper.map(userModel, Object.class);
113
    }
114
115
    private UserModelInterface getUserModel(Integer id, UsersOptionsInterface userOptions) {
116
        final UserModelInterface userModel = this.usersService.find(id, userOptions);
117
        if (userModel == null) {
118
            throw new NotFoundException();
119
        }
120
        return userModel;
121
    }
122
123
    private UserModelInterface getUserModel(Integer id) {
124
        return this.getUserModel(id, this.usersOptionsBuilder.forAuth());
125
    }
126
}
127