Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
DAAExample
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
Santiago Gómez Vilar
DAAExample
Commits
dfac988d
You need to sign in or sign up before continuing.
Commit
dfac988d
authored
Mar 06, 2019
by
Santi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added get to pets resource
parent
0e11c308
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
182 additions
and
7 deletions
+182
-7
DAAExampleApplication.java
src/main/java/es/uvigo/esei/daa/DAAExampleApplication.java
+8
-7
PetsDAO.java
src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java
+44
-0
Pet.java
src/main/java/es/uvigo/esei/daa/entities/Pet.java
+64
-0
PetsResource.java
src/main/java/es/uvigo/esei/daa/rest/PetsResource.java
+66
-0
No files found.
src/main/java/es/uvigo/esei/daa/DAAExampleApplication.java
View file @
dfac988d
package
es
.
uvigo
.
esei
.
daa
;
package
es
.
uvigo
.
esei
.
daa
;
import
static
java
.
util
.
stream
.
Collectors
.
toSet
;
import
es.uvigo.esei.daa.rest.PeopleResource
;
import
es.uvigo.esei.daa.rest.PetsResource
;
import
es.uvigo.esei.daa.rest.UsersResource
;
import
javax.ws.rs.ApplicationPath
;
import
javax.ws.rs.core.Application
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.Set
;
import
java.util.stream.Stream
;
import
java.util.stream.Stream
;
import
javax.ws.rs.ApplicationPath
;
import
static
java
.
util
.
stream
.
Collectors
.
toSet
;
import
javax.ws.rs.core.Application
;
import
es.uvigo.esei.daa.rest.PeopleResource
;
import
es.uvigo.esei.daa.rest.UsersResource
;
/**
/**
* Configuration of the REST application. This class includes the resources and
* Configuration of the REST application. This class includes the resources and
...
@@ -26,7 +26,8 @@ public class DAAExampleApplication extends Application {
...
@@ -26,7 +26,8 @@ public class DAAExampleApplication extends Application {
public
Set
<
Class
<?>>
getClasses
()
{
public
Set
<
Class
<?>>
getClasses
()
{
return
Stream
.
of
(
return
Stream
.
of
(
PeopleResource
.
class
,
PeopleResource
.
class
,
UsersResource
.
class
UsersResource
.
class
,
PetsResource
.
class
).
collect
(
toSet
());
).
collect
(
toSet
());
}
}
...
...
src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java
0 → 100644
View file @
dfac988d
package
es
.
uvigo
.
esei
.
daa
.
dao
;
import
es.uvigo.esei.daa.entities.Pet
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
public
class
PetsDAO
extends
DAO
{
private
final
static
Logger
LOG
=
Logger
.
getLogger
(
PetsDAO
.
class
.
getName
());
public
Pet
get
(
int
id
)
throws
DAOException
,
IllegalArgumentException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"SELECT * FROM pet WHERE id=?"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setInt
(
1
,
id
);
try
(
final
ResultSet
result
=
statement
.
executeQuery
())
{
if
(
result
.
next
())
{
return
rowToEntity
(
result
);
}
else
{
throw
new
IllegalArgumentException
(
"Invalid id"
);
}
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error getting a person"
,
e
);
throw
new
DAOException
(
e
);
}
}
private
Pet
rowToEntity
(
ResultSet
row
)
throws
SQLException
{
return
new
Pet
(
row
.
getInt
(
"id"
),
row
.
getInt
(
"owner"
),
row
.
getString
(
"name"
)
);
}
}
src/main/java/es/uvigo/esei/daa/entities/Pet.java
0 → 100644
View file @
dfac988d
package
es
.
uvigo
.
esei
.
daa
.
entities
;
public
class
Pet
{
private
int
id
;
private
String
name
;
private
int
owner
;
// Constructor needed for the JSON conversion
Pet
()
{}
/**
* Constructs a new instance of {@link Pet}
*
* @param id idintefier of the pet
* @param name name of the pet
* @param owner identifier of the pet ownet
*/
public
Pet
(
int
id
,
int
owner
,
String
name
)
{
this
.
id
=
id
;
this
.
name
=
name
;
this
.
owner
=
owner
;
}
public
int
getId
()
{
return
id
;
}
public
void
setId
(
int
id
)
{
this
.
id
=
id
;
}
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
int
getOwner
()
{
return
owner
;
}
public
void
setOwner
(
int
owner
)
{
this
.
owner
=
owner
;
}
public
boolean
equals
(
Object
object
)
{
if
(
this
==
object
)
return
true
;
if
(!(
object
instanceof
Pet
))
return
false
;
if
(!
super
.
equals
(
object
))
return
false
;
Pet
pet
=
(
Pet
)
object
;
return
getId
()
==
pet
.
getId
()
&&
getOwner
()
==
pet
.
getOwner
()
&&
java
.
util
.
Objects
.
equals
(
getName
(),
pet
.
getName
());
}
public
int
hashCode
()
{
return
java
.
util
.
Objects
.
hash
(
super
.
hashCode
(),
getId
(),
getName
(),
getOwner
());
}
}
src/main/java/es/uvigo/esei/daa/rest/PetsResource.java
0 → 100644
View file @
dfac988d
package
es
.
uvigo
.
esei
.
daa
.
rest
;
import
es.uvigo.esei.daa.dao.DAOException
;
import
es.uvigo.esei.daa.dao.PetsDAO
;
import
es.uvigo.esei.daa.entities.Pet
;
import
javax.ws.rs.GET
;
import
javax.ws.rs.Path
;
import
javax.ws.rs.PathParam
;
import
javax.ws.rs.Produces
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
/**
* REST resource for managing pets.
*
* @author sgvilar.
*/
@Path
(
"/pets"
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
class
PetsResource
{
private
final
static
Logger
LOG
=
Logger
.
getLogger
(
PetsResource
.
class
.
getName
());
private
final
PetsDAO
dao
;
PetsResource
(
PetsDAO
dao
)
{
this
.
dao
=
dao
;
}
/**
* Returns a person with the provided identifier.
*
* @param id the identifier of the person to retrieve.
* @return a 200 OK response with a person that has the provided identifier.
* If the identifier does not corresponds with any user, a 400 Bad Request
* response with an error message will be returned. If an error happens
* while retrieving the list, a 500 Internal Server Error response with an
* error message will be returned.
*/
@GET
@Path
(
"/{id}"
)
public
Response
get
(
@PathParam
(
"id"
)
int
id
)
{
try
{
final
Pet
pet
=
this
.
dao
.
get
(
id
);
return
Response
.
ok
(
pet
).
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid pet id in get method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error getting a pet"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
}
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