Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 8549b80b

Přidáno uživatelem Petr Urban před asi 2 roky(ů)

#10228 - mírna úprava response json objektu pro namapování na react

Zobrazit rozdíly:

src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/UserController.java
13 13

  
14 14
/**
15 15
 * Controller for providing basic support for user management
16
 * @version 1.0
16
 *
17 17
 * @author Vaclav Hrabik, Jiri Trefil
18
 * @version 1.0
18 19
 */
19 20
@RestController
20 21
@RequestMapping("v2/user")
......
28 29

  
29 30
    /**
30 31
     * Method to register new user in the app
31
     * @param user  User who wants register in to the app
32
     * @return      message
32
     *
33
     * @param user User who wants register in to the app
34
     * @return message
33 35
     */
34 36
    @PostMapping(value = "/register")
35 37
    public ResponseEntity<String> registerUser(@RequestBody User user) {
36 38
        UserModelStatusCodes statusCode = userService.registerUser(user);
37
        return getResponseEntity(statusCode);
39
        return getResponseEntity(statusCode, null);
38 40
    }
39 41

  
40 42
    /**
41 43
     * Method to login user in to the app
42
     * @param user  User who wants to login in to the app
43
     * @return      message
44
     *
45
     * @param user User who wants to login in to the app
46
     * @return message
44 47
     */
45 48
    @PostMapping(value = "/login")
46 49
    public ResponseEntity<String> loginUser(@RequestBody User user) {
47 50
        UserModelStatusCodes statusCode = userService.loginUser(user);
48
        return getResponseEntity(statusCode);
51

  
52
        String jwtToken = null;
53
        if (statusCode.statusCode == 200)
54
            jwtToken = "token";
55

  
56
        return getResponseEntity(statusCode, jwtToken);
49 57
    }
50 58

  
51 59
    /**
52 60
     * Method to logout user from the app
53
     * @param user  User who wants logout from the app
54
     * @return      message after logout
61
     *
62
     * @param user User who wants logout from the app
63
     * @return message after logout
55 64
     */
56 65
    @PostMapping(value = "/logout")
57 66
    public ResponseEntity<String> logoutUser(@RequestBody User user) {
58 67
        UserModelStatusCodes statusCode = userService.logoutUser(user);
59
        return getResponseEntity(statusCode);
68
        return getResponseEntity(statusCode, null);
60 69
    }
61 70

  
62 71
    /**
63 72
     * Method to create response
64
     * @param statusCode    UserModelStatusCodes code
65
     * @return              ResponseEntity with code and msg
73
     *
74
     * @param statusCode UserModelStatusCodes code
75
     * @return ResponseEntity with code and msg
66 76
     */
67
    private ResponseEntity<String> getResponseEntity(UserModelStatusCodes statusCode) {
68
        String message = this.generateResponseObject(statusCode);
77
    private ResponseEntity<String> getResponseEntity(UserModelStatusCodes statusCode, String jwtToken) {
78
        String message = this.generateResponseObject(statusCode, jwtToken);
69 79
        int code = statusCode.statusCode;
70 80
        return new ResponseEntity<>(message, HttpStatus.valueOf(code));
71 81
    }
72 82

  
73 83
    /**
74 84
     * Method to create JSON object
75
     * @param code  UserModelStatusCodes code
76
     * @return      String that represents JSON object
85
     *
86
     * @param code UserModelStatusCodes code
87
     * @return String that represents JSON object
77 88
     */
78
    private String generateResponseObject(UserModelStatusCodes code){
89
    private String generateResponseObject(UserModelStatusCodes code, String jwtToken) {
79 90
        HashMap<String, String> json = new HashMap<>();
80 91
        json.put("msg", code.label);
92
        if (jwtToken != null) {
93
            json.put("jwtToken", jwtToken);
94
        }
81 95
        return JSONBuilder.buildJson(json);
82

  
83 96
    }
84 97

  
85 98

  
86

  
87 99
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/dials/UserModelStatusCodes.java
16 16
    public final String label;
17 17
    public final int statusCode;
18 18

  
19
    private UserModelStatusCodes(String s, int i) {
19
    UserModelStatusCodes(String s, int i) {
20 20
        this.label = s;
21 21
        this.statusCode = i;
22 22
    }
23

  
23 24
}

Také k dispozici: Unified diff