0

How would one implement a simple deletion Rest request in the Controller suchh that I can delete both by id and by name?

I have attempted to emmulate the This StackOverflow question but with no luck so far. Please see code below.

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public void removeStudent(@PathVariable("id") int id){
   studentService.removeStudent(id);
}

@RequestMapping(method = RequestMethod.DELETE)
public void removeStudent(@RequestParam(value="name") String name){
   studentService.removeStudent(name);
}

Deleting by id works perfectly, however when attempting to delete by name - nothing works.

I attempted to DELETE just the specific record for a name with the following as per the previous question mentioned:

http://localhost:8080/students?name=FOUR

But it brings EVERYTHING back...

EDIT

I believe me code for removing the entry is incorrect.

I am attempting to remove an entry from a HashMap:

 private static Map<Integer, Student> students;

    static {

        students = new HashMap<Integer, Student>(){

            {
                // int id, String name, String school, String course
                put(1, new Student(1,"Adam", 24, "School1", "Physics"));
                put(2, new Student(2,"Luke", 27, "School1", "Electronics"));
                put(3, new Student(3,"Mick", 56, "School1", "Trains"));
            }
        };
    }

@Override
    public void removeStudent(String name) { this.students.remove(name); }
physicsboy
  • 5,656
  • 17
  • 70
  • 119
  • And have you verified that your delete query is actually working... – M. Deinum Oct 06 '17 at 10:52
  • what's the code of `removeStudent(name)` method? maybe the problem is in your service + repository and not in your controller... have you tried to debug your code? – davioooh Oct 06 '17 at 10:56
  • Are you getting any error ? Could you please share the stacktrace ? – VelNaga Oct 06 '17 at 11:18
  • See my edit for the code implementing the deletion. I believe I am doing it completely wrong. – physicsboy Oct 06 '17 at 12:27
  • In your map the key is id of student and so the remove will try to remove the value based on key. When you try `removeStudent(name)` with `String` parameter of a student's name it will not find any corresponding entry for it and will not remove anything. – madteapot Oct 06 '17 at 12:46
  • @Setu - I spotted that one myself a little while ago, but couldn't think of a way around the problem. Unsure if you can use something like .contains(name) to search the value associated with the key etc. – physicsboy Oct 06 '17 at 12:48
  • The contains method still looks for the given parameter in keys rather than values. You can loop through the keys and retrieve their corresponding entries (Student) to check their names against the input parameter to find the match(es) and then delete them. – madteapot Oct 06 '17 at 12:55
  • @Setu - I had a look at doing that but couldn't quite get it to work properly :-( – physicsboy Oct 06 '17 at 12:57
  • If you tried something already than add it to the question and clarify as to what you mean by 'couldn't quite get it to work properly'. Also from my previous comment there is not `contains` method on HashMap; however there are `containsKey` and `containsValue` methods on it. – madteapot Oct 06 '17 at 13:05
  • @Setu - Yeah, I can't figure it out whatsoever at the moment – physicsboy Oct 06 '17 at 13:53

1 Answers1

0

The issue indeed is your removeStudent(String name) implementation. Hashmaps don't work the way you've intended. As stated in the comments an element from a map can be removed using the key, which is of type Integer in this case.

One approach to remove all students with a certain name would be to use an Iterator on the HashMap (if running with Java 7) or a lambda function (if you're using Java 8) and find the actual student that has the specified name.

Example (Java 8):

students.stream()
 .filter(student -> name.equals(student.getName())
 .forEach(matching -> students.remove(matching));
Markus
  • 1,016
  • 1
  • 8
  • 23