Ben-Xav / holycraap

BACKEND In search of the one and only Holy Craap - a treasure also known as "Big Whoop(TM)" - a new adventure game written in Java and JavaScript, using Spring and React.
GNU General Public License v3.0
1 stars 0 forks source link

Fix POST /add API route and add tests #25

Closed xavierhardy closed 4 years ago

xavierhardy commented 4 years ago

There are several things wrong with the PeopleController.

    @PostMapping(path = "/add")
    public @ResponseBody
    String addNewPeople(@RequestParam String name
            , @RequestParam int currentHp, @RequestParam int hpMax, @RequestParam int currentMp, @RequestParam int mpMax) {

        People p = new People();
        p.setName(name);
        p.setCurrentHp(currentHp);
        p.setHpMax(hpMax);
        p.setCurrentMp(currentMp);
        p.setMpMax(mpMax);
        peopleService.saveInBase(p);
        return name + "added to database !";
    }
  1. Request parameters (a.k.a. query parameters) are the &-separated arguments you pass in the URL (after ?)

    https://example.com/search?something=42&other=somevalue

    Here something and other would be the request parameters. Usually, API routes expecting the POST HTTP verb are expected to contain their arguments in the content (also called body) in a serialized form (e.g. most commonly today: JSON). Apparently, request parameters can also be used when sending FORM content (which is not much used in modern service AFAIK).

  2. The People methods used here are undefined.

  3. Nitpicking: the controllers should be in a separate package (e.g. controller). As they are probably annotated with @Controller, their current location is not a problem, but it's inconsistent.

  4. The whole concept of having the user add character themselves make little sense to me, unless this is meant for administrators only or a game editor, which brings me back to the problem of defining the project scope and requirements.

BenMorant commented 4 years ago

As I said last week, it was a class I created following this Spring.io guide : https://spring.io/guides/gs/accessing-data-mysql/ . I just wanted to try if something other than "YO!" could be displayed on a simple way. it's never been a working class (hero), and has never found its "raison d'être". I've always wanted to remove it.