Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement REST API #14

Merged
merged 10 commits into from
Sep 20, 2024
3 changes: 3 additions & 0 deletions src/main/kotlin/com/hibob/academy/rest_api/Owner.kt
Original file line number Diff line number Diff line change
@@ -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)
77 changes: 77 additions & 0 deletions src/main/kotlin/com/hibob/academy/rest_api/OwnerResource.kt
Original file line number Diff line number Diff line change
@@ -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/envelopes")
RonAzar marked this conversation as resolved.
Show resolved Hide resolved
@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{
RonAzar marked this conversation as resolved.
Show resolved Hide resolved
owner == null ->{
throw NotFoundException("No owner with id $ownerId")
}
ownerId == 123L ->{
Response.status(Response.Status.UNAUTHORIZED).build()
}
else ->{
Response.ok(owner.toString()).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)
Response.ok(updatedOwner).build()
}
else{
RonAzar marked this conversation as resolved.
Show resolved Hide resolved
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 if(owner != null) {
RonAzar marked this conversation as resolved.
Show resolved Hide resolved
owners.remove(owner)
Response.ok("${owner.toString()} Has been removed").build()
}
else
throw NotFoundException("No owner with id $ownerId")
}

}
5 changes: 5 additions & 0 deletions src/main/kotlin/com/hibob/academy/rest_api/Pet.kt
Original file line number Diff line number Diff line change
@@ -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)
96 changes: 96 additions & 0 deletions src/main/kotlin/com/hibob/academy/rest_api/PetController.kt
Original file line number Diff line number Diff line change
@@ -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/envelopes")
RonAzar marked this conversation as resolved.
Show resolved Hide resolved
@Produces(MediaType.APPLICATION_JSON)

class PetResource {
// GET: Retrieve pet by ID
//Example-> "http://localhost:8080/api/ron/pets/envelopes/1"
@GET
@Path("/{petId}")
RonAzar marked this conversation as resolved.
Show resolved Hide resolved
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("Pet Type: ${pet.type}, Name: ${pet.name}, Company ID: ${pet.companyId}, Date of Arrival: ${pet.dateOfArrival}").build()
RonAzar marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

// 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")
}
}
}
Loading