diff --git a/src/main/kotlin/com/hibob/academy/rest_api/Owner.kt b/src/main/kotlin/com/hibob/academy/rest_api/Owner.kt new file mode 100644 index 000000000..a2a928b97 --- /dev/null +++ b/src/main/kotlin/com/hibob/academy/rest_api/Owner.kt @@ -0,0 +1,3 @@ +package com.hibob.academy.rest_api + +data class Owner(val ownerId: Long, val name: String, val companyId: Long, val employeeId: Long) \ No newline at end of file diff --git a/src/main/kotlin/com/hibob/academy/rest_api/OwnerResource.kt b/src/main/kotlin/com/hibob/academy/rest_api/OwnerResource.kt new file mode 100644 index 000000000..62b9ccb44 --- /dev/null +++ b/src/main/kotlin/com/hibob/academy/rest_api/OwnerResource.kt @@ -0,0 +1,77 @@ +package com.hibob.academy.rest_api + +import jakarta.ws.rs.* +import jakarta.ws.rs.core.MediaType +import jakarta.ws.rs.core.Response +import org.springframework.stereotype.Controller + +val owners = mutableListOf( + Owner(1L,"Ron", 101L, 10L), + Owner(2L,"Or", 102L, 11L), + Owner(3L,"Noam", 103L, 12L) +) + + +@Controller +@Path("/api/ron/owners") +@Produces(MediaType.APPLICATION_JSON) +class OwnerResource { + + //GET: Retrieve all owners + @GET + fun getAll() : Response { + return Response.ok(owners).build() + } + + //GET: Retrieve owner by ownerId + @GET + @Path("/{ownerId}") + fun getById(@PathParam("ownerId") ownerId: Long): Response { + val owner = owners.find { it.ownerId == ownerId } + return when{ + owner == null ->{ + throw NotFoundException("No owner with id $ownerId") + } + ownerId == 123L ->{ + Response.status(Response.Status.UNAUTHORIZED).build() + } + else ->{ + //Sending JSON + Response.ok(owner).build() + } + } + } + + @POST + fun addOwner(newOwner : Owner) : Response { + owners.add(newOwner) + return Response.status(Response.Status.CREATED).entity(newOwner).build() + } + + //PUT: Update a owner by ownerId + @PUT + @Path("/{ownerId}") + fun updateOwner(@PathParam("ownerId") ownerId: Long, updatedOwner: Owner): Response { + val index = owners.indexOfFirst { it.ownerId == ownerId } + return if (index>=0){ + owners[index] = updatedOwner.copy(ownerId = ownerId) + //Sending JSON + Response.ok(updatedOwner).build() + } + else{ + throw NotFoundException("No owner with id $ownerId") + } + } + + //DELETE: Delete an owner by ownerId + @DELETE + @Path("/{ownerId}") + fun deleteOwner(@PathParam("ownerId") ownerId: Long): Response { + val owner = owners.find { it.ownerId == ownerId } + return owner?.let { + owners.remove(owner) + Response.ok("${owner.toString()} Has been removed").build() + }?: throw NotFoundException("No owner with id $ownerId") + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/hibob/academy/rest_api/Pet.kt b/src/main/kotlin/com/hibob/academy/rest_api/Pet.kt new file mode 100644 index 000000000..dc04031e4 --- /dev/null +++ b/src/main/kotlin/com/hibob/academy/rest_api/Pet.kt @@ -0,0 +1,5 @@ +package com.hibob.academy.rest_api + +import java.util.Date + +data class Pet(val petId: Long, val name: String, val type: String, val companyId: Int, val dateOfArrival: Date) \ No newline at end of file diff --git a/src/main/kotlin/com/hibob/academy/rest_api/PetController.kt b/src/main/kotlin/com/hibob/academy/rest_api/PetController.kt new file mode 100644 index 000000000..601d04842 --- /dev/null +++ b/src/main/kotlin/com/hibob/academy/rest_api/PetController.kt @@ -0,0 +1,96 @@ +package com.hibob.academy.rest_api + +import jakarta.ws.rs.* +import jakarta.ws.rs.core.MediaType +import jakarta.ws.rs.core.Response +import org.springframework.stereotype.Controller +import java.util.Date + +val pets = mutableListOf( + Pet(1L, "Buddy", "Dog", 101, Date()), + Pet(2L, "Whiskers", "Cat", 102, Date()), + Pet(3L, "Polly", "Parrot", 103, Date()) +) + +@Controller +@Path("/api/ron/pets") +@Produces(MediaType.APPLICATION_JSON) + +class PetResource { + // GET: Retrieve pet by ID + //Example-> "http://localhost:8080/api/ron/pets/envelopes/1" + @GET + @Path("/{petId}/type") + fun getPetType(@PathParam("petId") petId: Long): Response { + val pet = pets.find { it.petId == petId } + return when { + pet == null -> { + throw NotFoundException("Pet not found") // Pet not found + } + petId == 123L -> { + Response.status(Response.Status.UNAUTHORIZED).build() // Unauthorized for petId 123 + } + else -> { + Response.ok().entity(pet).build() + } + } + } + + // GET: Retrieve all pets + //Example-> "http://localhost:8080/api/ron/pets/envelopes" + @GET + fun getAllPets(): Response { + return Response.ok(pets).build() + } + + // POST: Add a new pet + //Example-> "http://localhost:8080/api/ron/pets/envelopes" + // "Content-Type: application/json" \ + // '{ + // "name": "Max", + // "type": "Dog", + // "companyId": 104, + // "dateOfArrival": "2024-09-06T12:00:00Z" + // }' + @POST + fun addPet(newPet: Pet): Response { + pets.add(newPet) + return Response.status(Response.Status.CREATED).entity(newPet).build() + } + + + // PUT: Update a pet by ID + //Example-> "http://localhost:8080/api/ron/pets/envelopes/1" \ + // "Content-Type: application/json" + // { + // "name": "Buddy Updated", + // "type": "Dog", + // "companyId": 101, + // "dateOfArrival": "2024-09-06T12:00:00Z" + // } + @PUT + @Path("/{petId}") + fun updatePet(@PathParam("petId") petId: Long, updatedPet: Pet): Response { + val index = pets.indexOfFirst { it.petId == petId } + return if (index >= 0) { + pets[index] = updatedPet.copy(petId = petId) // Keep the same pet ID + Response.ok(updatedPet).build() + } else { + throw NotFoundException("Pet not found") + } + } + + // DELETE: Delete a pet by ID + //Example-> "http://localhost:8080/api/ron/pets/envelopes/1" + @DELETE + @Path("/{petId}") + fun deletePet(@PathParam("petId") petId: Long): Response { + val pet = pets.find { it.petId == petId } + return if (pet != null) { + pets.remove(pet) + Response.ok("Pet with ID $petId deleted").build() + } else { + throw NotFoundException("Pet not found") + } + } +} \ No newline at end of file