package ge.edu.freeuni.sdp.iot.service.camera_object_recognizer.service;

import com.microsoft.azure.storage.StorageException;
import ge.edu.freeuni.sdp.iot.service.camera_object_recognizer.data.Repository;
import ge.edu.freeuni.sdp.iot.service.camera_object_recognizer.data.RepositoryFactory;
import ge.edu.freeuni.sdp.iot.service.camera_object_recognizer.model.ObjectEntity;
import ge.edu.freeuni.sdp.iot.service.camera_object_recognizer.proxy.ProxyFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Path("/houses/{house_id}/objects")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ObjectService {

    public Repository getRepository() throws StorageException {
        return RepositoryFactory.create();
    }

    public ProxyFactory getProxyFactory() {
        return ProxyFactory.getProxyFactory();
    }

    @GET
    public List<ObjectDo> stub(@PathParam("house_id") String houseId) throws StorageException {

        if (!houseExists(houseId))
            throw new NotFoundException();
        List<ObjectDo> result = new ArrayList<>();
        for (ObjectEntity obj : getRepository().getAll()) {
            result.add(obj.toDo());
        }
        return result;
    }

    @POST
    public ObjectDo addObject(@PathParam("house_id") String houseId, ObjectDo obj) throws StorageException {
        if (!houseExists(houseId) || obj.getType() == null)
            throw new NotFoundException();
        obj.setId(UUID.randomUUID().toString());
        try {
            getRepository().insertOrUpdate(ObjectEntity.fromDo(obj));
        } catch (StorageException e) {
            throw new InternalServerErrorException();
        }
        return obj;
    }

    @GET
    @Path("{object_id}")
    public ObjectDo getObjectById(@PathParam("house_id") String houseId,
                                  @PathParam("object_id") String objectId) throws StorageException {
        ObjectEntity entity = getRepository().find(objectId);
        if (!houseExists(houseId) || entity == null)
            throw new NotFoundException();
        return entity.toDo();
    }

    private boolean houseExists(String id) {
        return getProxyFactory()
                .getHouseRegistryService()
                .get(id);
    }
}

@@ -454,16 +454,61 @@
 ctory;%0A%0A
+import javax.validation.constraints.NotNull;%0A
 import j
@@ -521,16 +521,16 @@
 s.rs.*;%0A
-
 import j
@@ -1468,16 +1468,25 @@
 houseId,
+ @NotNull
  ObjectD
@@ -2242,32 +2242,32 @@
 undException();%0A
-
         return e
@@ -2283,24 +2283,558 @@
 o();%0A    %7D%0A%0A
+    @PUT%0A    @Path(%22%7Bobject_id%7D%22)%0A    public ObjectDo updateObjectById(@PathParam(%22house_id%22) String houseId,%0A                                     @PathParam(%22object_id%22) String objectId,%0A                                     @NotNull ObjectDo obj) throws StorageException %7B%0A        ObjectDo objectDo = getObjectById(houseId, objectId);%0A        // all errors are handled in get object by id%0A        objectDo.setType(obj.getType());%0A        getRepository().insertOrUpdate(ObjectEntity.fromDo(objectDo));%0A        return objectDo;%0A    %7D%0A%0A
     private 

Add update object by id.