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

Ron azar Resources and Services #22

Merged
merged 104 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
a34c508
Not Finshed yet
RonAzar Sep 8, 2024
23f3ba6
Class Exercise-SQL
RonAzar Sep 8, 2024
6224b5a
May's Review-the name of the file should be the date it was created
RonAzar Sep 10, 2024
5ab3350
May's Review-index should include the table name
RonAzar Sep 10, 2024
abe6e4d
May's Review-where id=2
RonAzar Sep 10, 2024
51edf29
May's Review- DEFAULT date value
RonAzar Sep 10, 2024
c18f7f4
May's Review- rename to id, the owner part is redundant in the contex…
RonAzar Sep 10, 2024
e7a5289
May's Review- should be not null
RonAzar Sep 10, 2024
ad5e85d
May's Review- employee_id is string in bob also should be not null
RonAzar Sep 10, 2024
934fdc7
May's Review- should be one index on both columns, and the name shoul…
RonAzar Sep 10, 2024
da4fb7d
May's Review- Thank's for the review!
RonAzar Sep 10, 2024
ea4d4ec
May's Review- Adding test migration
RonAzar Sep 10, 2024
f3be6db
Not finshed yet
RonAzar Sep 10, 2024
128a9ac
Create a function that retrieve all the pets by a given type (represe…
RonAzar Sep 10, 2024
20c9ff5
Create a function that retrieve all the owners from the owner table a…
RonAzar Sep 11, 2024
b0b9a68
May review- should be bigint in hibob
RonAzar Sep 11, 2024
788ccee
May review- There is no point in putting an index on a primary key
RonAzar Sep 11, 2024
6a6ff04
May review- maybe this file is causing you problems you can remove it
RonAzar Sep 11, 2024
031a669
Complete
RonAzar Sep 11, 2024
28c5b5a
unique index
RonAzar Sep 11, 2024
48a1047
Not finshed yet
RonAzar Sep 10, 2024
4d0de72
Create a function that retrieve all the pets by a given type (represe…
RonAzar Sep 10, 2024
f22849c
Create a function that retrieve all the owners from the owner table a…
RonAzar Sep 11, 2024
f12c49c
Complete class exercise
RonAzar Sep 11, 2024
ada0129
Complete class exercise
RonAzar Sep 11, 2024
48175af
Merge remote-tracking branch 'origin/RonAzar/JOOQ-Omer' into RonAzar/…
RonAzar Sep 11, 2024
ac1d80c
Complete class exercise
RonAzar Sep 11, 2024
e22c14e
Remove unique index from Pets
RonAzar Sep 11, 2024
398c88d
change unique index to index
RonAzar Sep 12, 2024
e7b747a
Change my enum class TYPE name to a more unique and representing name
RonAzar Sep 12, 2024
f178931
remove index from pets table
RonAzar Sep 12, 2024
f0f5bf1
Adding owner_id col to pets table
RonAzar Sep 12, 2024
d47c154
Fixing my SQL-Migration PR
RonAzar Sep 12, 2024
8654173
Lets move it to a different pr--> moved to my -Migration PR
RonAzar Sep 12, 2024
246d4e1
employeeId should be string field
RonAzar Sep 12, 2024
b12d769
id is fine instead if ownerId
RonAzar Sep 12, 2024
eac350f
Improve my tests-
RonAzar Sep 12, 2024
460a6cd
When we fetch data from the database we fetch by companyId(this is wh…
RonAzar Sep 12, 2024
24683cf
Updated code
RonAzar Sep 12, 2024
a773cab
Add an API that will receive a pet id and owner id
RonAzar Sep 12, 2024
10bee44
Last commit Jooq
RonAzar Sep 12, 2024
466a396
Merging from SQL branch
RonAzar Sep 12, 2024
39492a6
Nothing
RonAzar Sep 12, 2024
69f5657
Adding data class with all pet info- PetAllInfo
RonAzar Sep 12, 2024
715fd62
Adding ownerId to PetTable
RonAzar Sep 12, 2024
9a5f1ed
Complete JOOQ CRUD methods and corresponding tests.
RonAzar Sep 13, 2024
ffa9dae
Switched to RIGHT JOIN to retrieve all owners, including those withou…
RonAzar Sep 13, 2024
958720c
Removed Pet redundant data class, keeping the one with complete pet i…
RonAzar Sep 13, 2024
1b718e7
Spelling mistakes fixed
RonAzar Sep 13, 2024
78c459c
Removed `getPetByDetails` function and added `getPetById` function. T…
RonAzar Sep 14, 2024
ea30d4e
Fixed after last review
RonAzar Sep 14, 2024
bc31c81
Little changes
RonAzar Sep 14, 2024
2efd2c3
Removed companyId from `test GetAllPets function`() TEST
RonAzar Sep 14, 2024
362ce75
Adding 2 new Controllers
RonAzar Sep 14, 2024
dcfa484
pets path
RonAzar Sep 14, 2024
a20441a
add some comments
RonAzar Sep 14, 2024
9b029fd
Adding new POST API Method to Owner
RonAzar Sep 14, 2024
e4c38ef
Adding new POST API Method to get allOwners by company id
RonAzar Sep 14, 2024
a5c287a
Complete Owner Resource
RonAzar Sep 15, 2024
e27f06e
Complete Pet Resource
RonAzar Sep 15, 2024
76f0fad
I should use a left join because it ensures that the query returns th…
RonAzar Sep 15, 2024
04954a4
Update JOOQ Version
RonAzar Sep 15, 2024
dbe23e8
Use resource now
RonAzar Sep 15, 2024
98392d9
Added new Resources
RonAzar Sep 15, 2024
453168d
Completed implementation of Pet and Owner services and resources. Add…
RonAzar Sep 15, 2024
a440455
Added few more tests to PetDao
RonAzar Sep 15, 2024
bf6bf33
Added few more tests to PetDao & OwnerDao
RonAzar Sep 15, 2024
a77d66f
Removed ?: -1 adding !!
RonAzar Sep 15, 2024
69e03c1
Removed A
RonAzar Sep 16, 2024
de6c08b
Removed All resources files and services
RonAzar Sep 16, 2024
0cd64ef
Add All resources files and services
RonAzar Sep 16, 2024
4a5fab2
remove all changes from this migration it should be like this
RonAzar Sep 16, 2024
675d9dc
remove all changes from this migration it should be like this
RonAzar Sep 16, 2024
dc1b5aa
Merge remote-tracking branch 'origin/sql/lesson01' into sql/lesson01
RonAzar Sep 16, 2024
df3c502
Merge branch 'master' into sql/lesson01
RonAzar Sep 16, 2024
fc8059b
Merge branch 'sql/lesson01' into RonAzar/JOOQ-Omer
RonAzar Sep 16, 2024
71f844a
Merge branch 'sql/lesson01' into RonAzar/JOOQ-Omer
RonAzar Sep 16, 2024
56262d6
Merge remote-tracking branch 'origin/RonAzar/JOOQ-Omer' into RonAzar/…
RonAzar Sep 16, 2024
e26b9f3
Merge branch 'RonAzar/JOOQ-Omer' into RonAzar/Resource-And-Service-Omer
RonAzar Sep 16, 2024
8ec4cdc
Add 2 migrations vaccine and vaccineToPet (#25)
RonAzar Sep 16, 2024
c521e6b
Change my pet service and resource
RonAzar Sep 16, 2024
4a9c9ec
Add 2 migrations vaccine and vaccineToPet (#25)
RonAzar Sep 16, 2024
cd8b3a5
Merge remote-tracking branch 'origin/sql/lesson01' into sql/lesson01
RonAzar Sep 16, 2024
982967f
Start pet service tests
RonAzar Sep 16, 2024
c241403
Complete all service tests
RonAzar Sep 16, 2024
7de5b6a
Merge branch 'RonAzar/JOOQ-Omer' into RonAzar/Resource-And-Service-Omer
RonAzar Sep 16, 2024
c85eef4
Merge Jooq into current Branch
RonAzar Sep 16, 2024
85975e0
Merge branch 'master' into RonAzar/Resource-And-Service-Omer
RonAzar Sep 17, 2024
88a0f29
fixed test in github
RonAzar Sep 17, 2024
6d9e3cb
Fixed Pet resource and service
RonAzar Sep 17, 2024
dc19be2
Fixed Owner & Pet resource and service
RonAzar Sep 17, 2024
4014908
Fixed Owner & Pet resource and service
RonAzar Sep 17, 2024
b29382d
Merge branch 'master' into RonAzar/Resource-And-Service-Omer
RonAzar Sep 17, 2024
83c8009
Change data classes to reasonable names
RonAzar Sep 17, 2024
f32692a
Update package path
RonAzar Sep 17, 2024
e2aec31
Fixed - Why 2 twice companyId in path?
RonAzar Sep 18, 2024
1fcf6f7
Fixed - Response.ok is enough
RonAzar Sep 18, 2024
05e52f3
Fixed - No need to specify the next param
RonAzar Sep 18, 2024
a0ff880
Fixed - You already have @Produces in the begging of the class, this …
RonAzar Sep 18, 2024
3554473
Fixed - Why 2 twice companyId in path?
RonAzar Sep 18, 2024
0af0add
Remove unnecessary '/'
RonAzar Sep 18, 2024
35ff3ce
Fixed- What we will do with this String?
RonAzar Sep 18, 2024
2af9298
Fixed - enter between functions
RonAzar Sep 18, 2024
3368774
Fixed - tests
RonAzar Sep 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/main/kotlin/com/hibob/academy/resource/OwnerResource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.hibob.academy.resource
import com.hibob.academy.dao.OwnerRecord
import com.hibob.academy.service.OwnerService
import jakarta.ws.rs.*
import jakarta.ws.rs.core.MediaType
import jakarta.ws.rs.core.Response
import org.springframework.stereotype.Controller

@Controller // Marks the class as a controller, meaning it's responsible for handling incoming HTTP requests
@Path("/api/ron/azar/owner/") // Defines the base URL path that this controller will handle
@Produces(MediaType.APPLICATION_JSON) // Specifies that the responses produced by this controller will be in JSON format
@Consumes(MediaType.APPLICATION_JSON) // Specifies that this endpoint accepts JSON input
class OwnerResource (private val ownerService: OwnerService){

@GET
@Path("getAllOwners/{companyId}")
fun getAllOwners(@PathParam("companyId") companyId: Long): Response {
val owners = ownerService.getAllOwners(companyId)
return Response.ok(owners).build()
}

@POST
@Path("insertNewOwner")
fun insertNewOwner(newOwner: OwnerRecord): Response {
ownerService.insertOwner(newOwner)
return Response.status(Response.Status.CREATED).entity("Owner successfully inserted").build()
}

@GET
@Path("companies/{companyId}/pets/{petId}/owner")
fun getOwnerByPetId(
@PathParam("petId") petId: Long,
@PathParam("companyId") companyId: Long
): Response {
val owner = ownerService.getOwnerByPetId(petId, companyId)
return Response.ok(owner).build()
}

@GET
@Path("companies/{companyId}/employees/{employeeId}/owner")
fun getOwnerByEmployeeIdAndCompanyId(
@PathParam("employeeId") employeeId: String,
@PathParam("companyId") companyId: Long
): Response {
val owner = ownerService.getOwnerByEmployeeIdAndCompanyId(employeeId, companyId)
return Response.ok(owner).build()
}
}
60 changes: 60 additions & 0 deletions src/main/kotlin/com/hibob/academy/resource/PetsResource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.hibob.academy.resource

import com.hibob.academy.dao.PetRecord
import com.hibob.academy.dao.PetType
import com.hibob.academy.service.PetService
import jakarta.ws.rs.Consumes
import jakarta.ws.rs.GET
import jakarta.ws.rs.POST
import jakarta.ws.rs.PUT
import jakarta.ws.rs.Path
import jakarta.ws.rs.PathParam
import jakarta.ws.rs.Produces
import jakarta.ws.rs.core.MediaType
import jakarta.ws.rs.core.Response
import org.springframework.stereotype.Controller

@Controller // Marks the class as a controller, meaning it's responsible for handling incoming HTTP requests
@Path("/api/ron/azar/pets/") // Defines the base URL path that this controller will handle
@Produces(MediaType.APPLICATION_JSON) // Specifies that the responses produced by this controller will be in JSON format
@Consumes(MediaType.APPLICATION_JSON) // Specifies that this endpoint accepts JSON input
class PetsResource (private val petService: PetService){
@GET
@Path("companies/{companyId}/pets/type/{petType}")
fun getAllPetsByType(@PathParam("petType") petType: PetType, @PathParam("companyId") companyId: Long): Response {
val pet = petService.getAllPetsByType(petType, companyId)
return Response.ok(pet).build()
}

@GET
@Path("getAllPets/{companyId}")
fun getAllPets(@PathParam("companyId") companyId: Long): Response {
val petsList = petService.getAllPets(companyId)
return Response.ok(petsList).build()
}

@GET
@Path("companies/{companyId}/pets/{petId}")
fun getPetById(@PathParam("petId") petId: Long, @PathParam("companyId") companyId: Long): Response {
val pet = petService.getPetById(petId, companyId)
return Response.ok(pet).build()
}

@POST
@Path("insertNewPet")
fun insertNewPet(newPet: PetRecord): Response {
petService.insertNewPet(newPet)
return Response.ok(newPet).build()
}

@PUT
@Path("pets/{petId}/owner/{newOwnerId}/company/{companyId}")
fun updatePetOwnerId(
@PathParam("petId") petId: Long,
@PathParam("newOwnerId") newOwnerId: Long,
@PathParam("companyId") companyId: Long
): Response {
petService.updatePetOwnerId(petId, newOwnerId, companyId)
return Response.ok().build()
}
}
34 changes: 34 additions & 0 deletions src/main/kotlin/com/hibob/academy/service/OwnerService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.hibob.academy.service

import com.hibob.academy.dao.OwnerRecord
import com.hibob.academy.dao.OwnerDao
import com.hibob.academy.dao.OwnerData
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
class OwnerService @Autowired constructor(private val ownerDao: OwnerDao) {
fun insertOwner(newOwner: OwnerRecord): Long {
val newOwnerId = ownerDao.insertNewOwner(newOwner)
if (newOwnerId < 0L) {
throw IllegalArgumentException("Owner insertion failed...")
}
return newOwnerId
}

fun getOwnerByEmployeeIdAndCompanyId(employeeId: String, companyId: Long): OwnerData {
val owner = ownerDao.getOwnerByEmployeeIdAndCompanyId(employeeId, companyId)
?: throw NoSuchElementException("No owner with id $employeeId found")
return owner
}
RonAzar marked this conversation as resolved.
Show resolved Hide resolved

fun getAllOwners(companyId: Long): List<OwnerData> {
return ownerDao.getAllOwners(companyId)
}

fun getOwnerByPetId(petId: Long, companyId: Long): OwnerData {
val owner = ownerDao.getOwnerByPetId(petId, companyId)
?: throw NoSuchElementException("Pet $petId has no owner with company id $companyId")
return owner
}
}
46 changes: 46 additions & 0 deletions src/main/kotlin/com/hibob/academy/service/PetService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.hibob.academy.service

import com.hibob.academy.dao.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
class PetService @Autowired constructor(private val petDao: PetDao) {
fun getAllPets(companyId: Long): List<PetData> {
return petDao.getAllPets(companyId)
}

fun insertNewPet(newPet: PetRecord): Long {
// Insert the new pet and return the generated ID or -1 if the operation failed
val petId = petDao.insertNewPet(newPet)
if (petId < 0L) {
throw IllegalArgumentException("Pet insertion failed...")
}
return petId
}

fun getPetById(petId: Long, companyId: Long): PetData {
val pet = petDao.getPetById(petId, companyId) ?: throw NoSuchElementException("Pet not found for the given ID")
return pet
}

fun getAllPetsByType(type: PetType, companyId: Long): List<PetData> {
return petDao.getAllPetsByType(type, companyId)
}

fun updatePetOwnerId(petId: Long, petOwnerId: Long, companyId: Long) {
// Retrieve the pet by ID and companyId
val pet = petDao.getPetById(petId, companyId)
?: throw NoSuchElementException("Pet not found for the given ID")
// Ensure the pet doesn't already have an owner
if (pet.ownerId != null) {
throw IllegalArgumentException("Pet already has an owner")
}

// Update the ownerId if it is currently null
val rowsAffected = petDao.updatePetOwnerId(petId, petOwnerId, companyId)
if (rowsAffected == 0) {
throw IllegalArgumentException("Failed to update pet's owner. The pet may already have an owner.")
}
}
}
72 changes: 36 additions & 36 deletions src/test/kotlin/com/hibob/academy/resource/ExampleResourceTest.kt
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
package com.hibob.academy.resource

import com.hibob.academy.dao.Example
import com.hibob.academy.service.ExampleService
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers
import org.junit.jupiter.api.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.whenever
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.http.HttpStatus
import kotlin.random.Random

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
internal class ExampleResourceTest {

@Autowired
lateinit var restTemplate: TestRestTemplate

@MockBean
lateinit var exampleService: ExampleService

@Test
fun test() {
val example = Example(Random.nextLong(), Random.nextLong(), "a")
whenever(exampleService.get(1)).thenReturn(example)
val response = restTemplate.getForEntity("/example/1", ExampleResponse::class.java)
assertThat(response.statusCode, Matchers.`is`(HttpStatus.OK))
assertThat(response.body, `is`(ExampleResponse(example)))
}
}
//package com.hibob.academy.resource
//
//import com.hibob.academy.dao.Example
//import com.hibob.academy.service.ExampleService
//import org.hamcrest.CoreMatchers.`is`
//import org.hamcrest.MatcherAssert.assertThat
//import org.hamcrest.Matchers
//import org.junit.jupiter.api.Test
//import org.mockito.kotlin.any
//import org.mockito.kotlin.whenever
//import org.springframework.beans.factory.annotation.Autowired
//import org.springframework.boot.test.context.SpringBootTest
//import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
//import org.springframework.boot.test.mock.mockito.MockBean
//import org.springframework.boot.test.web.client.TestRestTemplate
//import org.springframework.http.HttpStatus
//import kotlin.random.Random
//
//@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
//internal class ExampleResourceTest {
//
// @Autowired
// lateinit var restTemplate: TestRestTemplate
//
// @MockBean
// lateinit var exampleService: ExampleService
//
// @Test
// fun test() {
// val example = Example(Random.nextLong(), Random.nextLong(), "a")
// whenever(exampleService.get(1)).thenReturn(example)
// val response = restTemplate.getForEntity("/example/1", ExampleResponse::class.java)
// assertThat(response.statusCode, Matchers.`is`(HttpStatus.OK))
// assertThat(response.body, `is`(ExampleResponse(example)))
// }
//}
78 changes: 78 additions & 0 deletions src/test/kotlin/com/hibob/academy/service/OwnerServiceTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.hibob.academy.service

import com.hibob.academy.dao.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever

class OwnerServiceTest{
private val petId = 10L
private val ownerId = 123L
private val employeeId = "1234578"
private val newOwner= OwnerRecord("Ron", employeeId, 9)
private val ownerData = OwnerData(ownerId,"Ron", employeeId, 9)
private val companyId = 9L

private val ownerDao = mock<OwnerDao>()
private val ownerService = OwnerService(ownerDao)


@Test
fun `insert Owner -- insert successfully`()
{
whenever(ownerDao.insertNewOwner(newOwner)).thenReturn(10L)
val resultOwnerId = ownerService.insertOwner(newOwner)
assertEquals(10L, resultOwnerId)
}

@Test
fun `get Owner By Employee Id And Company Id -- Success`(){
whenever(ownerDao.getOwnerByEmployeeIdAndCompanyId(any(), any())).thenReturn(ownerData)

val resultOwner = ownerService.getOwnerByEmployeeIdAndCompanyId(employeeId, companyId)
assertEquals(ownerData, resultOwner)
}

@Test
fun `get Owner By Employee Id And Company Id -- Failure`(){
whenever(ownerDao.getOwnerByEmployeeIdAndCompanyId(any(), any())).thenReturn(null)

val errorMessage = assertThrows<NoSuchElementException> {
ownerService.getOwnerByEmployeeIdAndCompanyId(employeeId, companyId)
}
assertEquals(errorMessage.message, "No owner with id $employeeId found")
}

@Test
fun `get all owners - Success`(){
whenever(ownerDao.getAllOwners(companyId)).thenReturn(listOf(ownerData))
val result = ownerService.getAllOwners(companyId)
assertNotNull(result)
assertEquals(newOwner.ownerName, result[0].ownerName)
assertEquals(1, result.size)
}

@Test
fun `get Owner By Pet Id -- Success`() {
whenever(ownerDao.getOwnerByPetId(petId, companyId)).thenReturn(ownerData)

val resultOwner = ownerService.getOwnerByPetId(petId, companyId)

assertEquals(ownerData, resultOwner)
}

@Test
fun `get Owner By Pet Id -- Failure`() {
whenever(ownerDao.getOwnerByPetId(petId, companyId)).thenReturn(null)

val errorMessage = assertThrows<NoSuchElementException> {
ownerService.getOwnerByPetId(petId, companyId)
}

assertEquals("Pet $petId has no owner with company id $companyId", errorMessage.message)
}
}
Loading
Loading