package pl.mycalories.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pl.mycalories.model.AbstractModel;
import pl.mycalories.service.AbstractService;

import java.util.List;

public abstract class AbstractController<T extends AbstractModel, S extends AbstractService<T, Long>> {

    private S service;

    public AbstractController(S service) {
        this.service = service;
    }

    @GetMapping
    public @ResponseBody
    ResponseEntity<List<T>> getAll() {
        List<T> objects = service.getAll();

        return new ResponseEntity<List<T>>(HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public @ResponseBody
    ResponseEntity<T> get(@PathVariable Long id) {
        T obj = service.findById(id);

        return new ResponseEntity<T>(obj, HttpStatus.OK);
    }

    @PostMapping
    public @ResponseBody
    ResponseEntity<T> create(@RequestBody T obj) {
        T createdObj = service.save(obj);

        return new ResponseEntity<T>(HttpStatus.OK);
    }

    @PutMapping("/{id}")
    public @ResponseBody
    ResponseEntity<T> update(@PathVariable Long id, @RequestBody T obj) {
        obj.setId(id);
        obj = service.save(obj);

        return new ResponseEntity<T>(obj, HttpStatus.OK);
    }

}

@@ -410,13 +410,15 @@
   pr
-ivate
+otected
  S s
@@ -687,16 +687,25 @@
 ist%3CT%3E%3E(
+objects, 
 HttpStat
@@ -1113,16 +1113,28 @@
 tity%3CT%3E(
+createdObj, 
 HttpStat
@@ -1330,32 +1330,32 @@
 e.save(obj);%0D%0A%0D%0A
-
         return n
@@ -1406,11 +1406,256 @@
    %7D%0D%0A%0D%0A
+    @DeleteMapping(%22/%7Bid%7D%22)%0D%0A    public @ResponseBody%0D%0A    ResponseEntity%3CT%3E delete (@PathVariable Long id) %7B%0D%0A        T obj = service.findById(id);%0D%0A%0D%0A        service.delete(obj);%0D%0A        return new ResponseEntity%3CT%3E(HttpStatus.OK);%0D%0A    %7D%0D%0A%0D%0A
 %7D%0D%0A

change service modifier to protected so classes who inherits can use it + add missing return objects + add delete method