[UI] Support data registration
[validation.git] / ui / src / main / java / org / akraino / validation / ui / controller / TimeslotController.java
@@ -19,7 +19,7 @@ package org.akraino.validation.ui.controller;
 import java.util.List;
 
 import org.akraino.validation.ui.entity.Timeslot;
-import org.akraino.validation.ui.service.TimeslotService;
+import org.akraino.validation.ui.service.DbAdapter;
 import org.onap.portalsdk.core.controller.RestrictedBaseController;
 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
 import org.onap.portalsdk.core.web.support.UserUtils;
@@ -27,19 +27,20 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 
 @Controller
-@RequestMapping("/api/v1/timeslots")
-public class TimeslotsController extends RestrictedBaseController {
+@RequestMapping("/api/v1/timeslot")
+public class TimeslotController extends RestrictedBaseController {
 
-    private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(TimeslotsController.class);
+    private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(TimeslotController.class);
 
     @Autowired
-    TimeslotService service;
+    DbAdapter service;
 
-    public TimeslotsController() {
+    public TimeslotController() {
         super();
     }
 
@@ -49,8 +50,30 @@ public class TimeslotsController extends RestrictedBaseController {
             return new ResponseEntity<>(service.getTimeslots(), HttpStatus.OK);
         } catch (Exception e) {
             LOGGER.error(EELFLoggerDelegate.errorLogger,
-                    "Error occured when trying to retrieve timeslots. " + UserUtils.getStackTrace(e));
+                    "Error occured when trying to retrieve timeslots." + UserUtils.getStackTrace(e));
         }
         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
     }
+
+    @RequestMapping(value = { "/" }, method = RequestMethod.POST)
+    public ResponseEntity<Timeslot> createTimeslot(@RequestBody Timeslot timeslot) {
+        try {
+            service.saveTimeslot(timeslot);
+            return new ResponseEntity<>(timeslot, HttpStatus.OK);
+        } catch (Exception e) {
+            LOGGER.error(EELFLoggerDelegate.errorLogger, "Creation of timeslot failed. " + UserUtils.getStackTrace(e));
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
+        }
+    }
+
+    @RequestMapping(value = { "/" }, method = RequestMethod.DELETE)
+    public ResponseEntity<Boolean> deleteTimeslot(@RequestBody Timeslot timeslot) {
+        try {
+            service.deleteTimeslot(timeslot);
+            return new ResponseEntity<>(true, HttpStatus.OK);
+        } catch (Exception e) {
+            LOGGER.error(EELFLoggerDelegate.errorLogger, "Deletion of timeslot failed. " + UserUtils.getStackTrace(e));
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
+        }
+    }
 }