Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
bspastoriza19-esi-solutions
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Breixo Senra Pastoriza
bspastoriza19-esi-solutions
Commits
dc5935d4
Commit
dc5935d4
authored
Oct 24, 2025
by
Breixo Senra
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Endpoints canVaccinate y create funcionando
parent
f8cb9edc
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
138 additions
and
4 deletions
+138
-4
VaccinationResource.java
...main/java/es/uvigo/esei/xcs/rest/VaccinationResource.java
+67
-0
VetResource.java
rest/src/main/java/es/uvigo/esei/xcs/rest/VetResource.java
+3
-3
VaccinationService.java
...in/java/es/uvigo/esei/xcs/service/VaccinationService.java
+68
-1
No files found.
rest/src/main/java/es/uvigo/esei/xcs/rest/VaccinationResource.java
0 → 100644
View file @
dc5935d4
package
es
.
uvigo
.
esei
.
xcs
.
rest
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
javax.ejb.EJB
;
import
javax.ws.rs.Consumes
;
import
javax.ws.rs.GET
;
import
javax.ws.rs.POST
;
import
javax.ws.rs.Path
;
import
javax.ws.rs.PathParam
;
import
javax.ws.rs.Produces
;
import
javax.ws.rs.QueryParam
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
import
es.uvigo.esei.xcs.domain.entities.Vaccination
;
import
es.uvigo.esei.xcs.service.VaccinationService
;
@Path
(
"vaccination"
)
@Consumes
(
MediaType
.
APPLICATION_JSON
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
class
VaccinationResource
{
@EJB
private
VaccinationService
vaccinationService
;
@GET
@Path
(
"canVaccinate/{petId}/{vaccineId}"
)
public
Response
canVaccinate
(
@PathParam
(
"petId"
)
Long
petId
,
@PathParam
(
"vaccineId"
)
Long
vaccineId
)
{
Date
now
=
new
Date
();
Boolean
result
=
vaccinationService
.
canVaccinate
(
petId
,
vaccineId
,
now
);
return
Response
.
ok
(
result
).
build
();
}
@POST
@Path
(
"create/{petId}/{vaccineId}"
)
public
Response
createVaccination
(
@PathParam
(
"petId"
)
Long
petId
,
@PathParam
(
"vaccineId"
)
Long
vaccineId
,
@QueryParam
(
"date"
)
String
textDate
// opcional, formato yyyy-MM-dd
)
{
Date
date
=
new
Date
();
if
(
textDate
!=
null
)
{
try
{
date
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
).
parse
(
textDate
);
}
catch
(
ParseException
e
)
{
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
"Invalid date format, expected yyyy-MM-dd"
).
build
();
}
}
try
{
Vaccination
vaccination
=
vaccinationService
.
create
(
petId
,
vaccineId
,
date
);
return
Response
.
ok
(
vaccination
).
build
();
}
catch
(
IllegalArgumentException
e
)
{
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
e
.
getMessage
()).
build
();
}
}
}
rest/src/main/java/es/uvigo/esei/xcs/rest/VetResource.java
View file @
dc5935d4
...
...
@@ -178,12 +178,12 @@ public class VetResource {
@Path
(
"pets/{petIdentifierType}/{petIdentifierValue}/vaccination"
)
@POST
public
Response
registerVaccination
(
@QueryParam
(
"date"
)
String
date
,
@QueryParam
(
"date"
)
Date
date
,
VaccinationCreationData
vaccinationData
)
{
Vaccination
vaccination
=
this
.
vaccinationService
.
create
(
vaccinationData
.
getPetId
()
.
intValue
()
,
vaccinationData
.
getVaccineId
()
.
intValue
()
,
vaccinationData
.
getPetId
(),
vaccinationData
.
getVaccineId
(),
date
);
...
...
service/src/main/java/es/uvigo/esei/xcs/service/VaccinationService.java
View file @
dc5935d4
...
...
@@ -7,15 +7,19 @@ import java.text.ParseException;
import
java.text.SimpleDateFormat
;
import
java.util.List
;
import
javax.annotation.security.PermitAll
;
import
javax.ejb.Stateless
;
import
javax.persistence.EntityManager
;
import
javax.persistence.PersistenceContext
;
import
es.uvigo.esei.xcs.domain.entities.MultidoseVaccine
;
import
es.uvigo.esei.xcs.domain.entities.PeriodicVaccine
;
import
es.uvigo.esei.xcs.domain.entities.Pet
;
import
es.uvigo.esei.xcs.domain.entities.Vaccination
;
import
es.uvigo.esei.xcs.domain.entities.Vaccine
;
@Stateless
@PermitAll
public
class
VaccinationService
{
@PersistenceContext
EntityManager
em
;
...
...
@@ -40,7 +44,7 @@ public class VaccinationService {
}
public
Vaccination
create
(
int
petId
,
int
vaccineId
,
String
textDate
)
{
/*
public Vaccination create(int petId, int vaccineId, String textDate) {
Pet pet = requireNonNull(em.find(Pet.class, petId), "Pet can't be null");
Vaccine vaccine = requireNonNull(em.find(Vaccine.class, vaccineId), "Vaccine can't be null");
Date date = new Date();
...
...
@@ -53,12 +57,26 @@ public class VaccinationService {
}
}
Vaccination vaccination = new Vaccination(pet, vaccine, date);
em.persist(vaccination);
return vaccination;
}*/
public
Vaccination
create
(
Long
petId
,
Long
vaccineId
,
Date
date
)
{
Pet
pet
=
requireNonNull
(
em
.
find
(
Pet
.
class
,
petId
),
"Pet can't be null"
);
Vaccine
vaccine
=
requireNonNull
(
em
.
find
(
Vaccine
.
class
,
vaccineId
),
"Vaccine can't be null"
);
if
(!
canVaccinate
(
petId
,
vaccineId
,
date
))
{
throw
new
IllegalArgumentException
(
"This vaccination cannot be created due to vaccine rules"
);
}
Vaccination
vaccination
=
new
Vaccination
(
pet
,
vaccine
,
date
);
em
.
persist
(
vaccination
);
return
vaccination
;
}
public
Vaccination
updateDate
(
int
vaccinationId
,
Date
date
)
{
Vaccination
vaccination
=
em
.
find
(
Vaccination
.
class
,
vaccinationId
);
requireNonNull
(
vaccination
,
"Vaccination can't be null"
);
...
...
@@ -74,4 +92,53 @@ public class VaccinationService {
}
public
Boolean
canVaccinate
(
Long
petId
,
Long
vaccineId
,
Date
date
)
{
Pet
pet
=
em
.
find
(
Pet
.
class
,
petId
);
Vaccine
vaccine
=
em
.
find
(
Vaccine
.
class
,
vaccineId
);
if
(
pet
==
null
||
vaccine
==
null
)
return
false
;
List
<
Vaccination
>
prevVaccinations
=
em
.
createQuery
(
"SELECT v FROM Vaccination v WHERE v.pet.id = :petId AND v.vaccine.id = :vaccineId ORDER BY v.date DESC"
,
Vaccination
.
class
)
.
setParameter
(
"petId"
,
petId
)
.
setParameter
(
"vaccineId"
,
vaccineId
)
.
getResultList
();
if
(
vaccine
instanceof
MultidoseVaccine
)
{
MultidoseVaccine
multi
=
(
MultidoseVaccine
)
vaccine
;
Integer
doses
=
multi
.
getDoses
();
if
(
doses
==
null
)
return
false
;
return
prevVaccinations
.
size
()
<
doses
;
}
else
if
(
vaccine
instanceof
PeriodicVaccine
)
{
PeriodicVaccine
periodic
=
(
PeriodicVaccine
)
vaccine
;
if
(
prevVaccinations
.
isEmpty
())
return
true
;
Vaccination
last
=
prevVaccinations
.
get
(
0
);
if
(
last
.
getDate
()
==
null
||
date
==
null
)
return
false
;
long
diffDays
;
switch
(
periodic
.
getPeriodicType
())
{
case
YEARS:
diffDays
=
periodic
.
getPeriode
()
*
365L
;
break
;
case
MONTHS:
diffDays
=
periodic
.
getPeriode
()
*
30L
;
break
;
case
DAYS:
diffDays
=
periodic
.
getPeriode
();
break
;
default
:
return
false
;
}
long
diffMillis
=
date
.
getTime
()
-
last
.
getDate
().
getTime
();
return
diffMillis
>=
diffDays
*
24L
*
60L
*
60L
*
1000L
;
}
else
{
// MONODOSE
return
prevVaccinations
.
isEmpty
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment