Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mod4-wp-2023-2024-pokemon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Morais Fonseca, Claudenir (UT-EEMCS)
mod4-wp-2023-2024-pokemon
Merge requests
!203
Issue
#18
,
#19
resolved
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Issue
#18
,
#19
resolved
s3126099/mod4-wp-2023-2024-pokemon:request-sorting
into
practical-session-3
Overview
0
Commits
18
Pipelines
0
Changes
5
Open
Malnačs, E. (Ernests, Student B-TCS)
requested to merge
s3126099/mod4-wp-2023-2024-pokemon:request-sorting
into
practical-session-3
9 months ago
Overview
0
Commits
18
Pipelines
0
Changes
5
Expand
Added pagination and sorting to the /api/pokemon endpoint
0
0
Merge request reports
Viewing commit
8f7dde1e
Prev
Next
Show latest version
5 files
+
176
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
5
Search (e.g. *.vue) (Ctrl+P)
8f7dde1e
Added a GET request handler for /api/pokemon endpoint
· 8f7dde1e
Ernests Malnacs
authored
9 months ago
src/main/java/nl/utwente/mod4/pokemon/dao/PokemonDao.java
0 → 100644
+
112
−
0
Options
package
nl.utwente.mod4.pokemon.dao
;
import
com.fasterxml.jackson.core.util.DefaultPrettyPrinter
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.ObjectWriter
;
import
jakarta.ws.rs.BadRequestException
;
import
jakarta.ws.rs.NotFoundException
;
import
java.io.File
;
import
java.io.IOException
;
import
java.time.Instant
;
import
java.time.ZoneId
;
import
java.time.format.DateTimeFormatter
;
import
java.util.*
;
import
nl.utwente.mod4.pokemon.Utils
;
import
nl.utwente.mod4.pokemon.model.Pokemon
;
import
nl.utwente.mod4.pokemon.model.Trainer
;
public
enum
PokemonDao
{
INSTANCE
;
private
static
final
String
ORIGINAL_POKEMON
=
Utils
.
getAbsolutePathToResources
()
+
"/default-pokemon-dataset.json"
;
private
static
final
String
POKEMON
=
Utils
.
getAbsolutePathToResources
()
+
"/pokemon.json"
;
private
final
DateTimeFormatter
dateFormatter
=
DateTimeFormatter
.
ofPattern
(
"dd/MM/yyyy"
);
private
HashMap
<
String
,
Pokemon
>
pokemons
=
new
HashMap
<>();
public
void
delete
(
String
id
)
{
if
(
pokemons
.
containsKey
(
id
))
{
pokemons
.
remove
(
id
);
}
else
{
throw
new
NotFoundException
(
"Pokemon '"
+
id
+
"' not found."
);
}
}
public
List
<
Pokemon
>
getPokemon
(
int
pageSize
,
int
pageNumber
)
{
var
list
=
new
ArrayList
<>(
pokemons
.
values
());
return
(
List
<
Pokemon
>)
Utils
.
pageSlice
(
list
,
pageSize
,
pageNumber
);
}
public
Pokemon
getTrainer
(
String
id
)
{
var
pt
=
pokemons
.
get
(
id
);
if
(
pt
==
null
)
{
throw
new
NotFoundException
(
"Pokemon '"
+
id
+
"' not found!"
);
}
return
pt
;
}
public
void
load
()
throws
IOException
{
ObjectMapper
mapper
=
new
ObjectMapper
();
File
source
=
existsPokemon
()
?
new
File
(
POKEMON
)
:
new
File
(
ORIGINAL_POKEMON
);
Pokemon
[]
arr
=
mapper
.
readValue
(
source
,
Pokemon
[].
class
);
Arrays
.
stream
(
arr
).
forEach
(
pokemon
->
pokemons
.
put
(
pokemon
.
id
,
pokemon
));
}
public
void
save
()
throws
IOException
{
ObjectMapper
mapper
=
new
ObjectMapper
();
ObjectWriter
writer
=
mapper
.
writer
(
new
DefaultPrettyPrinter
());
File
destination
=
new
File
(
POKEMON
);
writer
.
writeValue
(
destination
,
pokemons
.
values
());
}
private
boolean
existsPokemon
()
{
File
f
=
new
File
(
POKEMON
);
return
f
.
exists
()
&&
!
f
.
isDirectory
();
}
private
int
getMaxId
()
{
Set
<
String
>
ids
=
pokemons
.
keySet
();
return
ids
.
isEmpty
()
?
0
:
ids
.
stream
()
.
map
(
Integer:
:
parseInt
)
.
max
(
Integer:
:
compareTo
)
.
get
();
}
public
Pokemon
create
(
Pokemon
newPokemon
)
{
String
nextId
=
""
+
(
getMaxId
()
+
1
);
newPokemon
.
id
=
nextId
;
Instant
now
=
Instant
.
now
();
newPokemon
.
created
=
dateFormatter
.
format
(
now
.
atZone
(
ZoneId
.
systemDefault
()).
toLocalDateTime
());
newPokemon
.
lastUpDate
=
dateFormatter
.
format
(
now
.
atZone
(
ZoneId
.
systemDefault
()).
toLocalDateTime
());
pokemons
.
put
(
nextId
,
newPokemon
);
return
newPokemon
;
}
public
Pokemon
replace
(
Pokemon
updated
)
{
if
(!
updated
.
checkIsValid
())
throw
new
BadRequestException
(
"Invalid pokemon."
);
if
(
pokemons
.
get
(
updated
.
id
)
==
null
)
throw
new
NotFoundException
(
"Pokemon id '"
+
updated
.
id
+
"' not found."
);
Instant
now
=
Instant
.
now
();
updated
.
created
=
pokemons
.
get
(
updated
.
id
).
created
;
updated
.
lastUpDate
=
dateFormatter
.
format
(
now
.
atZone
(
ZoneId
.
systemDefault
()).
toLocalDateTime
());
pokemons
.
put
(
updated
.
id
,
updated
);
return
updated
;
}
public
int
getTotalPokemon
()
{
return
pokemons
.
keySet
().
size
();
}
}
Loading