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
Noelia García Hervella
daaexample
Commits
4dfb13fc
Commit
4dfb13fc
authored
Mar 19, 2020
by
Noelia García Hervella
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pets
Se han añadido las acciones de crear mascota, modificar y eliminar mascotas.
parent
ff8c98f5
Changes
26
Show whitespace changes
Inline
Side-by-side
Showing
26 changed files
with
920 additions
and
4 deletions
+920
-4
app-routing.module.ts
src/main/angular/src/app/app-routing.module.ts
+23
-1
app.module.ts
src/main/angular/src/app/app.module.ts
+1
-1
people-list.component.html
.../people/components/people-list/people-list.component.html
+1
-0
people-list.component.ts
...es/people/components/people-list/people-list.component.ts
+6
-1
pets-form.component.html
...odules/pets/components/pets-form/pets-form.component.html
+22
-0
pets-form.component.scss
...odules/pets/components/pets-form/pets-form.component.scss
+0
-0
pets-form.component.spec.ts
...les/pets/components/pets-form/pets-form.component.spec.ts
+25
-0
pets-form.component.ts
.../modules/pets/components/pets-form/pets-form.component.ts
+55
-0
pets-list.component.html
...odules/pets/components/pets-list/pets-list.component.html
+22
-0
pets-list.component.scss
...odules/pets/components/pets-list/pets-list.component.scss
+0
-0
pets-list.component.spec.ts
...les/pets/components/pets-list/pets-list.component.spec.ts
+25
-0
pets-list.component.ts
.../modules/pets/components/pets-list/pets-list.component.ts
+34
-0
pets-main.component.html
...odules/pets/components/pets-main/pets-main.component.html
+11
-0
pets-main.component.scss
...odules/pets/components/pets-main/pets-main.component.scss
+0
-0
pets-main.component.spec.ts
...les/pets/components/pets-main/pets-main.component.spec.ts
+25
-0
pets-main.component.ts
.../modules/pets/components/pets-main/pets-main.component.ts
+84
-0
pet.model.ts
src/main/angular/src/app/modules/pets/models/pet.model.ts
+7
-0
pets-routing.module.ts
src/main/angular/src/app/modules/pets/pets-routing.module.ts
+16
-0
pets.module.ts
src/main/angular/src/app/modules/pets/pets.module.ts
+22
-0
pets.service.spec.ts
...ngular/src/app/modules/pets/services/pets.service.spec.ts
+12
-0
pets.service.ts
...ain/angular/src/app/modules/pets/services/pets.service.ts
+41
-0
DAAExampleApplication.java
src/main/java/es/uvigo/esei/daa/DAAExampleApplication.java
+3
-1
PetsDAO.java
src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java
+172
-0
Pet.java
src/main/java/es/uvigo/esei/daa/entities/Pet.java
+121
-0
PetsResource.java
src/main/java/es/uvigo/esei/daa/rest/PetsResource.java
+189
-0
createdFiles.lst
...-compiler-plugin/compile/default-compile/createdFiles.lst
+3
-0
No files found.
src/main/angular/src/app/app-routing.module.ts
View file @
4dfb13fc
...
@@ -23,6 +23,8 @@ import {UnauthenticatedGuard} from './guards/unauthenticated.guard';
...
@@ -23,6 +23,8 @@ import {UnauthenticatedGuard} from './guards/unauthenticated.guard';
import
{
AuthenticatedGuard
}
from
'./guards/authenticated.guard'
;
import
{
AuthenticatedGuard
}
from
'./guards/authenticated.guard'
;
import
{
LoginPanelComponent
}
from
'./components/login-panel/login-panel.component'
;
import
{
LoginPanelComponent
}
from
'./components/login-panel/login-panel.component'
;
import
{
MainPanelComponent
}
from
'./components/main-panel/main-panel.component'
;
import
{
MainPanelComponent
}
from
'./components/main-panel/main-panel.component'
;
import
{
PetsFormComponent
}
from
'./modules/pets/components/pets-form/pets-form.component'
;
const
routes
:
Routes
=
[
const
routes
:
Routes
=
[
{
{
...
@@ -44,9 +46,29 @@ const routes: Routes = [
...
@@ -44,9 +46,29 @@ const routes: Routes = [
{
{
path
:
'people'
,
path
:
'people'
,
loadChildren
:
()
=>
import
(
'./modules/people/people.module'
).
then
(
m
=>
m
.
PeopleModule
)
loadChildren
:
()
=>
import
(
'./modules/people/people.module'
).
then
(
m
=>
m
.
PeopleModule
)
},
{
path
:
'listPets/:person'
,
redirectTo
:
':person'
,
pathMatch
:
'full'
},
{
path
:
':person'
,
loadChildren
:
()
=>
import
(
'./modules/pets/pets.module'
).
then
(
m
=>
m
.
PetsModule
)
}
}
]
]
}
},
/*{
path: 'pets/:person',
pathMatch: 'full',
component: PetsFormComponent,
canActivate: [AuthenticatedGuard]
}*/
];
];
@
NgModule
({
@
NgModule
({
...
...
src/main/angular/src/app/app.module.ts
View file @
4dfb13fc
...
@@ -33,7 +33,7 @@ import {AuthenticationInterceptor} from './interceptors/authentication.intercept
...
@@ -33,7 +33,7 @@ import {AuthenticationInterceptor} from './interceptors/authentication.intercept
declarations
:
[
declarations
:
[
AppComponent
,
AppComponent
,
LoginPanelComponent
,
LoginPanelComponent
,
MainPanelComponent
MainPanelComponent
,
],
],
imports
:
[
imports
:
[
AppRoutingModule
,
AppRoutingModule
,
...
...
src/main/angular/src/app/modules/people/components/people-list/people-list.component.html
View file @
4dfb13fc
...
@@ -32,6 +32,7 @@
...
@@ -32,6 +32,7 @@
<td
class=
"col-sm-3"
>
<td
class=
"col-sm-3"
>
<button
class=
"btn btn-primary edit"
(
click
)="
onEdit
(
person
)"
>
Edit
</button>
<button
class=
"btn btn-primary edit"
(
click
)="
onEdit
(
person
)"
>
Edit
</button>
<button
class=
"btn btn-warning delete"
(
click
)="
onDelete
(
person
)"
>
Delete
</button>
<button
class=
"btn btn-warning delete"
(
click
)="
onDelete
(
person
)"
>
Delete
</button>
<button
class=
"btn btn-success pets "
(
click
)="
pets
(
person
)"
>
Pets
</button>
</td>
</td>
</tr>
</tr>
</tbody>
</tbody>
...
...
src/main/angular/src/app/modules/people/components/people-list/people-list.component.ts
View file @
4dfb13fc
...
@@ -20,6 +20,7 @@
...
@@ -20,6 +20,7 @@
import
{
Component
,
EventEmitter
,
Input
,
OnInit
,
Output
}
from
'@angular/core'
;
import
{
Component
,
EventEmitter
,
Input
,
OnInit
,
Output
}
from
'@angular/core'
;
import
{
PersonModel
}
from
'../../models/person.model'
;
import
{
PersonModel
}
from
'../../models/person.model'
;
import
{
PeopleService
}
from
'../../services/people.service'
;
import
{
PeopleService
}
from
'../../services/people.service'
;
import
{
Router
}
from
'@angular/router'
;
@
Component
({
@
Component
({
selector
:
'app-people-list'
,
selector
:
'app-people-list'
,
...
@@ -36,7 +37,7 @@ export class PeopleListComponent {
...
@@ -36,7 +37,7 @@ export class PeopleListComponent {
@
Output
()
@
Output
()
public
readonly
delete
:
EventEmitter
<
PersonModel
>
;
public
readonly
delete
:
EventEmitter
<
PersonModel
>
;
public
constructor
()
{
public
constructor
(
private
readonly
router
:
Router
)
{
this
.
edit
=
new
EventEmitter
<
PersonModel
>
();
this
.
edit
=
new
EventEmitter
<
PersonModel
>
();
this
.
delete
=
new
EventEmitter
<
PersonModel
>
();
this
.
delete
=
new
EventEmitter
<
PersonModel
>
();
}
}
...
@@ -48,4 +49,8 @@ export class PeopleListComponent {
...
@@ -48,4 +49,8 @@ export class PeopleListComponent {
public
onDelete
(
person
:
PersonModel
)
{
public
onDelete
(
person
:
PersonModel
)
{
this
.
delete
.
emit
(
person
);
this
.
delete
.
emit
(
person
);
}
}
public
pets
(
person
:
PersonModel
){
this
.
router
.
navigate
([
'//listPets'
,
person
.
id
]);
}
}
}
src/main/angular/src/app/modules/pets/components/pets-form/pets-form.component.html
0 → 100644
View file @
4dfb13fc
<form
id=
"people-form"
class=
"mb-5 mb-10"
>
<input
name=
"id"
type=
"hidden"
value=
""
/>
<div
class=
"row"
>
<div
class=
"col-sm-4"
>
<input
name=
"name"
type=
"text"
value=
""
placeholder=
"Nombre"
class=
"form-control"
required
[(
ngModel
)]="
name
"
/>
</div>
<div
class=
"col-sm-5"
>
<input
name=
"kind"
type=
"text"
value=
""
placeholder=
"Tipo"
class=
"form-control"
required
[(
ngModel
)]="
kind
"
/>
</div>
<div
class=
"col-sm-3"
>
<button
id=
"btnSubmit"
class=
"btn btn-primary"
(
click
)="
onModify
()"
>
{{pet.id === undefined ? 'Crear' : 'Editar'}}
</button>
<button
id=
"btnClear"
class=
"btn"
(
click
)="
onClean
()"
>
Limpiar
</button>
</div>
</div>
</form>
src/main/angular/src/app/modules/pets/components/pets-form/pets-form.component.scss
0 → 100644
View file @
4dfb13fc
src/main/angular/src/app/modules/pets/components/pets-form/pets-form.component.spec.ts
0 → 100644
View file @
4dfb13fc
import
{
async
,
ComponentFixture
,
TestBed
}
from
'@angular/core/testing'
;
import
{
PetsFormComponent
}
from
'./pets-form.component'
;
describe
(
'PetsFormComponent'
,
()
=>
{
let
component
:
PetsFormComponent
;
let
fixture
:
ComponentFixture
<
PetsFormComponent
>
;
beforeEach
(
async
(()
=>
{
TestBed
.
configureTestingModule
({
declarations
:
[
PetsFormComponent
]
})
.
compileComponents
();
}));
beforeEach
(()
=>
{
fixture
=
TestBed
.
createComponent
(
PetsFormComponent
);
component
=
fixture
.
componentInstance
;
fixture
.
detectChanges
();
});
it
(
'should create'
,
()
=>
{
expect
(
component
).
toBeTruthy
();
});
});
src/main/angular/src/app/modules/pets/components/pets-form/pets-form.component.ts
0 → 100644
View file @
4dfb13fc
import
{
Component
,
OnInit
,
Input
,
Output
,
EventEmitter
}
from
'@angular/core'
;
import
{
ActivatedRoute
}
from
'@angular/router'
;
import
{
PetModel
}
from
'../../models/pet.model'
;
@
Component
({
selector
:
'app-pets-form'
,
templateUrl
:
'./pets-form.component.html'
,
styleUrls
:
[
'./pets-form.component.scss'
]
})
export
class
PetsFormComponent
{
public
activePet
:
PetModel
;
@
Input
()
public
owner
:
number
;
@
Output
()
public
readonly
clean
:
EventEmitter
<
never
>
;
@
Output
()
public
readonly
modify
:
EventEmitter
<
PetModel
>
;
public
name
:
string
;
public
kind
:
string
;
constructor
(
private
route
:
ActivatedRoute
)
{
this
.
clean
=
new
EventEmitter
<
never
>
();
this
.
modify
=
new
EventEmitter
<
PetModel
>
();
}
@
Input
()
public
set
pet
(
pet
:
PetModel
)
{
this
.
activePet
=
pet
;
this
.
name
=
pet
.
name
;
this
.
kind
=
pet
.
kind
;
}
public
get
pet
():
PetModel
{
return
this
.
activePet
;
}
public
onClean
()
{
this
.
clean
.
emit
();
}
public
onModify
()
{
this
.
modify
.
emit
({
id
:
this
.
pet
.
id
,
name
:
this
.
name
,
kind
:
this
.
kind
,
owner
:
this
.
owner
});
}
}
src/main/angular/src/app/modules/pets/components/pets-list/pets-list.component.html
0 → 100644
View file @
4dfb13fc
<table
id=
"people-list"
class=
"table"
>
<thead>
<tr
class=
"row"
>
<th
class=
"col-sm-4"
>
Nombre
</th>
<th
class=
"col-sm-5"
>
Tipo
</th>
<th
class=
"col-sm-3"
>
</th>
</tr>
</thead>
<tbody>
<tr
*
ngFor=
"let pet of pets"
class=
"row"
id=
"pet-{{pet.id}}"
>
<td
class=
"col-sm-4 name"
>
{{pet.name}}
</td>
<td
class=
"col-sm-5 surname"
>
{{pet.kind}}
</td>
<td
class=
"col-sm-3"
>
<button
class=
"btn btn-primary edit"
(
click
)="
onEdit
(
pet
)"
>
Edit
</button>
<button
class=
"btn btn-warning delete"
(
click
)="
onDelete
(
pet
)"
>
Delete
</button>
</td>
</tr>
</tbody>
</table>
src/main/angular/src/app/modules/pets/components/pets-list/pets-list.component.scss
0 → 100644
View file @
4dfb13fc
src/main/angular/src/app/modules/pets/components/pets-list/pets-list.component.spec.ts
0 → 100644
View file @
4dfb13fc
import
{
async
,
ComponentFixture
,
TestBed
}
from
'@angular/core/testing'
;
import
{
PetsListComponent
}
from
'./pets-list.component'
;
describe
(
'PetsListComponent'
,
()
=>
{
let
component
:
PetsListComponent
;
let
fixture
:
ComponentFixture
<
PetsListComponent
>
;
beforeEach
(
async
(()
=>
{
TestBed
.
configureTestingModule
({
declarations
:
[
PetsListComponent
]
})
.
compileComponents
();
}));
beforeEach
(()
=>
{
fixture
=
TestBed
.
createComponent
(
PetsListComponent
);
component
=
fixture
.
componentInstance
;
fixture
.
detectChanges
();
});
it
(
'should create'
,
()
=>
{
expect
(
component
).
toBeTruthy
();
});
});
src/main/angular/src/app/modules/pets/components/pets-list/pets-list.component.ts
0 → 100644
View file @
4dfb13fc
import
{
Component
,
EventEmitter
,
OnInit
,
Input
,
Output
}
from
'@angular/core'
;
import
{
PetModel
}
from
'../../models/pet.model'
;
@
Component
({
selector
:
'app-pets-list'
,
templateUrl
:
'./pets-list.component.html'
,
styleUrls
:
[
'./pets-list.component.scss'
]
})
export
class
PetsListComponent
{
@
Input
()
public
pets
:
PetModel
[]
=
[];
@
Output
()
public
readonly
delete
:
EventEmitter
<
PetModel
>
;
@
Output
()
public
readonly
edit
:
EventEmitter
<
PetModel
>
;
constructor
()
{
this
.
delete
=
new
EventEmitter
<
PetModel
>
();
this
.
edit
=
new
EventEmitter
<
PetModel
>
();
}
public
onDelete
(
pet
:
PetModel
)
{
this
.
delete
.
emit
(
pet
);
}
public
onEdit
(
pet
:
PetModel
)
{
this
.
edit
.
emit
(
pet
);
}
}
src/main/angular/src/app/modules/pets/components/pets-main/pets-main.component.html
0 → 100644
View file @
4dfb13fc
<!--
<h1 class="display-5 mt-3 mb-3">Personas</h1>
<app-people-form [person]="activePerson" (clean)="onCleanForm()" (modify)="onModifyForm($event)"></app-people-form>
<app-people-list [people]="people" (edit)="onEdit($event)" (delete)="onDelete($event)"></app-people-list>
-->
<h1
class=
"display-5 mt-3 mb-3"
>
Mascotas
</h1>
<app-pets-form
[
pet
]="
activePet
"
[
owner
]="
id
"
(
clean
)="
onCleanForm
()"
(
modify
)="
onModifyForm
($
event
)"
></app-pets-form>
<app-pets-list
[
pets
]="
pets
"
(
delete
)="
onDelete
($
event
)"
(
edit
)="
onEdit
($
event
)"
></app-pets-list>
<button
class=
"btn btn-primary edit"
(
click
)="
back
()"
>
Atrás
</button>
\ No newline at end of file
src/main/angular/src/app/modules/pets/components/pets-main/pets-main.component.scss
0 → 100644
View file @
4dfb13fc
src/main/angular/src/app/modules/pets/components/pets-main/pets-main.component.spec.ts
0 → 100644
View file @
4dfb13fc
import
{
async
,
ComponentFixture
,
TestBed
}
from
'@angular/core/testing'
;
import
{
PetsMainComponent
}
from
'./pets-main.component'
;
describe
(
'PetsMainComponent'
,
()
=>
{
let
component
:
PetsMainComponent
;
let
fixture
:
ComponentFixture
<
PetsMainComponent
>
;
beforeEach
(
async
(()
=>
{
TestBed
.
configureTestingModule
({
declarations
:
[
PetsMainComponent
]
})
.
compileComponents
();
}));
beforeEach
(()
=>
{
fixture
=
TestBed
.
createComponent
(
PetsMainComponent
);
component
=
fixture
.
componentInstance
;
fixture
.
detectChanges
();
});
it
(
'should create'
,
()
=>
{
expect
(
component
).
toBeTruthy
();
});
});
src/main/angular/src/app/modules/pets/components/pets-main/pets-main.component.ts
0 → 100644
View file @
4dfb13fc
import
{
Component
,
OnInit
}
from
'@angular/core'
;
import
{
PetModel
}
from
'../../models/pet.model'
;
import
{
PetsService
}
from
'../../services/pets.service'
;
import
{
map
,
mergeMap
}
from
'rxjs/operators'
;
import
{
ActivatedRoute
,
Router
}
from
'@angular/router'
;
@
Component
({
selector
:
'app-pets-main'
,
templateUrl
:
'./pets-main.component.html'
,
styleUrls
:
[
'./pets-main.component.scss'
]
})
export
class
PetsMainComponent
implements
OnInit
{
public
id
:
number
public
activePet
:
PetModel
;
public
pets
:
PetModel
[];
constructor
(
private
readonly
petsService
:
PetsService
,
private
route
:
ActivatedRoute
,
private
readonly
router
:
Router
)
{
this
.
pets
=
[];
this
.
clearActivePet
()
}
ngOnInit
()
{
this
.
id
=
this
.
route
.
snapshot
.
params
.
person
;
this
.
petsService
.
list
(
this
.
id
)
.
subscribe
(
pets
=>
this
.
pets
=
pets
);
}
public
onCleanForm
():
void
{
this
.
clearActivePet
();
}
public
clearActivePet
():
void
{
this
.
activePet
=
{
id
:
undefined
,
name
:
''
,
kind
:
''
,
owner
:
undefined
};
}
public
back
(){
this
.
router
.
navigate
([
'/'
]);
}
public
onDelete
(
pet
:
PetModel
):
void
{
if
(
confirm
(
`¿Estás seguro de que deseas eliminar a
${
pet
.
name
}
?`
))
{
this
.
petsService
.
delete
(
pet
)
.
pipe
(
mergeMap
(()
=>
this
.
petsService
.
list
(
pet
.
owner
))
)
.
subscribe
(
pets
=>
this
.
pets
=
pets
);
}
}
public
onEdit
(
pet
:
PetModel
):
void
{
this
.
activePet
=
pet
;
console
.
log
(
"Noe"
)
console
.
log
(
this
.
activePet
)
}
public
onModifyForm
(
pet
:
PetModel
):
void
{
if
(
pet
.
id
===
undefined
)
{
this
.
petsService
.
create
(
pet
)
.
pipe
(
mergeMap
(()
=>
this
.
petsService
.
list
(
pet
.
owner
))
)
.
subscribe
(
pets
=>
{
this
.
pets
=
pets
;
this
.
clearActivePet
();
});
}
else
{
this
.
petsService
.
modify
(
pet
)
.
pipe
(
mergeMap
(()
=>
this
.
petsService
.
list
(
pet
.
owner
))
)
.
subscribe
(
pets
=>
{
this
.
pets
=
pets
;
this
.
clearActivePet
();
});
}
}
}
src/main/angular/src/app/modules/pets/models/pet.model.ts
0 → 100644
View file @
4dfb13fc
export
class
PetModel
{
id
?:
number
;
name
:
string
;
kind
:
string
;
owner
:
number
;
}
\ No newline at end of file
src/main/angular/src/app/modules/pets/pets-routing.module.ts
0 → 100644
View file @
4dfb13fc
import
{
NgModule
}
from
'@angular/core'
;
import
{
RouterModule
,
Routes
}
from
'@angular/router'
;
import
{
PetsMainComponent
}
from
'./components/pets-main/pets-main.component'
;
const
routes
:
Routes
=
[
{
path
:
''
,
component
:
PetsMainComponent
}
];
@
NgModule
({
imports
:
[
RouterModule
.
forChild
(
routes
)],
exports
:
[
RouterModule
]
})
export
class
PetsRoutingModule
{
}
src/main/angular/src/app/modules/pets/pets.module.ts
0 → 100644
View file @
4dfb13fc
import
{
NgModule
}
from
'@angular/core'
;
import
{
CommonModule
}
from
'@angular/common'
;
import
{
PetsRoutingModule
}
from
'./pets-routing.module'
;
import
{
PetsListComponent
}
from
'./components/pets-list/pets-list.component'
;
import
{
PetsFormComponent
}
from
'./components/pets-form/pets-form.component'
;
import
{
PetsMainComponent
}
from
'./components/pets-main/pets-main.component'
;
import
{
FormsModule
}
from
'@angular/forms'
;
@
NgModule
({
declarations
:
[
PetsFormComponent
,
PetsListComponent
,
PetsMainComponent
],
imports
:
[
CommonModule
,
FormsModule
,
PetsRoutingModule
]
})
export
class
PetsModule
{
}
src/main/angular/src/app/modules/pets/services/pets.service.spec.ts
0 → 100644
View file @
4dfb13fc
import
{
TestBed
}
from
'@angular/core/testing'
;
import
{
PetsService
}
from
'./pets.service'
;
describe
(
'PetsService'
,
()
=>
{
beforeEach
(()
=>
TestBed
.
configureTestingModule
({}));
it
(
'should be created'
,
()
=>
{
const
service
:
PetsService
=
TestBed
.
get
(
PetsService
);
expect
(
service
).
toBeTruthy
();
});
});
src/main/angular/src/app/modules/pets/services/pets.service.ts
0 → 100644
View file @
4dfb13fc
import
{
Injectable
}
from
'@angular/core'
;
import
{
HttpClient
,
HttpHeaders
,
HttpParams
}
from
'@angular/common/http'
;
import
{
environment
}
from
'../../../../environments/environment'
;
import
{
Observable
}
from
'rxjs'
;
import
{
PetModel
}
from
'../models/pet.model'
;
@
Injectable
({
providedIn
:
'root'
})
export
class
PetsService
{
constructor
(
private
readonly
http
:
HttpClient
)
{
}
public
list
(
id
:
number
):
Observable
<
PetModel
[]
>
{
return
this
.
http
.
get
<
PetModel
[]
>
(
`
${
environment
.
restApi
}
/pets?owner=
${
id
}
`
);
}
public
delete
(
pet
:
PetModel
):
Observable
<
number
>
{
return
this
.
http
.
delete
<
number
>
(
`
${
environment
.
restApi
}
/pets/
${
pet
.
id
}
`
);
}
public
modify
(
pet
:
PetModel
):
Observable
<
PetModel
>
{
const
data
=
new
HttpParams
()
.
set
(
'name'
,
pet
.
name
)
.
set
(
'kind'
,
pet
.
kind
)
.
set
(
'owner'
,
pet
.
owner
.
toString
());
return
this
.
http
.
put
<
PetModel
>
(
`
${
environment
.
restApi
}
/pets/
${
pet
.
id
}
`
,
data
);
}
public
create
(
pet
:
PetModel
):
Observable
<
PetModel
>
{
const
data
=
new
HttpParams
()
.
set
(
'name'
,
pet
.
name
)
.
set
(
'kind'
,
pet
.
kind
)
.
set
(
'owner'
,
pet
.
owner
.
toString
());
return
this
.
http
.
post
<
PetModel
>
(
`
${
environment
.
restApi
}
/pets`
,
data
);
}
}
src/main/java/es/uvigo/esei/daa/DAAExampleApplication.java
View file @
4dfb13fc
...
@@ -12,6 +12,7 @@ import javax.ws.rs.core.Application;
...
@@ -12,6 +12,7 @@ import javax.ws.rs.core.Application;
import
es.uvigo.esei.daa.rest.PeopleResource
;
import
es.uvigo.esei.daa.rest.PeopleResource
;
import
es.uvigo.esei.daa.rest.UsersResource
;
import
es.uvigo.esei.daa.rest.UsersResource
;
import
es.uvigo.esei.daa.rest.PetsResource
;
/**
/**
* Configuration of the REST application. This class includes the resources and
* Configuration of the REST application. This class includes the resources and
...
@@ -26,7 +27,8 @@ public class DAAExampleApplication extends Application {
...
@@ -26,7 +27,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 @
4dfb13fc
package
es
.
uvigo
.
esei
.
daa
.
dao
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
javax.sound.sampled.SourceDataLine
;
import
es.uvigo.esei.daa.entities.Pet
;
/**
* DAO class for the {@link Pets} entities.
*
* @author Noelia García Hervella
*
*/
public
class
PetsDAO
extends
DAO
{
private
final
static
Logger
LOG
=
Logger
.
getLogger
(
PetsDAO
.
class
.
getName
());
/**
* Returns a list with all the pets persisted in the system.
*
* @return a list with all the pets persisted in the system.
* @throws DAOException if an error happens while retrieving the pets.
*/
public
List
<
Pet
>
list
(
int
id
)
throws
DAOException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"SELECT * FROM pets where owner=?"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setInt
(
1
,
id
);
try
(
final
ResultSet
result
=
statement
.
executeQuery
())
{
final
List
<
Pet
>
pets
=
new
LinkedList
<>();
while
(
result
.
next
())
{
pets
.
add
(
rowToEntity
(
result
));
}
return
pets
;
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error listing pets"
,
e
);
throw
new
DAOException
(
e
);
}
}
/**
* Removes a persisted pet from the system.
*
* @param id identifier of the pet to be deleted.
* @throws DAOException if an error happens while deleting the pet.
* @throws IllegalArgumentException if the provided id does not corresponds
* with any persisted pet.
*/
public
void
delete
(
int
id
)
throws
DAOException
,
IllegalArgumentException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"DELETE FROM pets WHERE id=?"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setInt
(
1
,
id
);
if
(
statement
.
executeUpdate
()
!=
1
)
{
throw
new
IllegalArgumentException
(
"Invalid id"
);
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error deleting a pet"
,
e
);
throw
new
DAOException
(
e
);
}
}
/**
* Persists a new pet in the system. An identifier will be assigned
* automatically to the new pet.
*
* @param name name of the new pet. Can't be {@code null}.
* @param kind surname of the new pet. Can't be {@code null}.
* @param owner id of the new owner. Can't be {@code null}.
* @return a {@link Pet} entity representing the persisted pet.
* @throws DAOException if an error happens while persisting the new pet.
* @throws IllegalArgumentException if the name or surname are {@code null}.
*/
public
Pet
add
(
String
name
,
String
kind
,
String
owner
)
throws
DAOException
,
IllegalArgumentException
{
if
(
name
==
null
||
kind
==
null
||
owner
==
null
)
{
throw
new
IllegalArgumentException
(
"name, kind and owner can't be null"
);
}
try
(
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"INSERT INTO pets VALUES(null, ?, ?, ?)"
;
try
(
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
,
Statement
.
RETURN_GENERATED_KEYS
))
{
statement
.
setString
(
1
,
name
);
statement
.
setString
(
2
,
kind
);
statement
.
setString
(
3
,
owner
);
if
(
statement
.
executeUpdate
()
==
1
)
{
try
(
ResultSet
resultKeys
=
statement
.
getGeneratedKeys
())
{
if
(
resultKeys
.
next
())
{
return
new
Pet
(
resultKeys
.
getInt
(
1
),
name
,
kind
,
Integer
.
parseInt
(
owner
));
}
else
{
LOG
.
log
(
Level
.
SEVERE
,
"Error retrieving inserted id"
);
throw
new
SQLException
(
"Error retrieving inserted id"
);
}
}
}
else
{
LOG
.
log
(
Level
.
SEVERE
,
"Error inserting value"
);
throw
new
SQLException
(
"Error inserting value"
);
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error adding a pet"
,
e
);
throw
new
DAOException
(
e
);
}
}
/**
* Modifies a pet previously persisted in the system. The pet will be
* retrieved by the provided id and its current name and surname will be
* replaced with the provided.
*
* @param pet a {@link pet} entity with the new data.
* @throws DAOException if an error happens while modifying the new pet.
* @throws IllegalArgumentException if the pet is {@code null}.
*/
public
void
modify
(
Pet
pet
)
throws
DAOException
,
IllegalArgumentException
{
if
(
pet
==
null
)
{
throw
new
IllegalArgumentException
(
"pet can't be null"
);
}
try
(
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"UPDATE pets SET name=?, kind=? WHERE id=?"
;
try
(
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setString
(
1
,
pet
.
getName
());
statement
.
setString
(
2
,
pet
.
getKind
());
statement
.
setInt
(
3
,
pet
.
getId
());
if
(
statement
.
executeUpdate
()
!=
1
)
{
throw
new
IllegalArgumentException
(
"name and surname can't be null"
);
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error modifying a pet"
,
e
);
throw
new
DAOException
();
}
}
private
Pet
rowToEntity
(
ResultSet
row
)
throws
SQLException
{
return
new
Pet
(
row
.
getInt
(
"id"
),
row
.
getString
(
"name"
),
row
.
getString
(
"kind"
),
row
.
getInt
(
"owner"
)
);
}
}
src/main/java/es/uvigo/esei/daa/entities/Pet.java
0 → 100644
View file @
4dfb13fc
package
es
.
uvigo
.
esei
.
daa
.
entities
;
import
static
java
.
util
.
Objects
.
requireNonNull
;
/**
* An entity that represents a pet.
*
* @author Noelia García Hervella
*/
public
class
Pet
{
private
int
id
;
private
String
name
;
private
String
kind
;
private
int
owner
;
// Constructor needed for the JSON conversion
Pet
()
{}
/**
* Constructs a new instance of {@link Pet}.
*
* @param id identifier of the pet.
* @param name name of the pet.
* @param surname surname of the pet.
*/
public
Pet
(
int
id
,
String
name
,
String
kind
,
int
owner
)
{
this
.
id
=
id
;
this
.
setName
(
name
);
this
.
setKind
(
kind
);
this
.
setOwner
(
owner
);
}
/**
* Returns the identifier of the pet.
*
* @return the identifier of the pet.
*/
public
int
getId
()
{
return
id
;
}
/**
* Returns the name of the pet.
*
* @return the name of the pet.
*/
public
String
getName
()
{
return
name
;
}
/**
* Set the name of this pet.
*
* @param name the new name of the pet.
* @throws NullPointerException if the {@code name} is {@code null}.
*/
public
void
setName
(
String
name
)
{
this
.
name
=
requireNonNull
(
name
,
"Name can't be null"
);
}
/**
* Returns the surname of the pet.
*
* @return the surname of the pet.
*/
public
String
getKind
()
{
return
kind
;
}
/**
* Set the surname of this pet.
*
* @param surname the new surname of the pet.
* @throws NullPointerException if the {@code surname} is {@code null}.
*/
public
void
setKind
(
String
surname
)
{
this
.
kind
=
requireNonNull
(
surname
,
"Kind can't be null"
);
}
/**
* Returns the id of the owner.
*
* @return the id of the owner.
*/
public
int
getOwner
()
{
return
owner
;
}
/**
* Set the id of this owner.
*
* @param owner the new id of the owner.
* @throws NullPointerException if the {@code surname} is {@code null}.
*/
public
void
setOwner
(
int
owner
)
{
this
.
owner
=
requireNonNull
(
owner
,
"The owner' id can't be null"
);
}
@Override
public
int
hashCode
()
{
final
int
prime
=
31
;
int
result
=
1
;
result
=
prime
*
result
+
id
;
return
result
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
)
return
false
;
if
(!(
obj
instanceof
Pet
))
return
false
;
Pet
other
=
(
Pet
)
obj
;
if
(
id
!=
other
.
id
)
return
false
;
return
true
;
}
}
src/main/java/es/uvigo/esei/daa/rest/PetsResource.java
0 → 100644
View file @
4dfb13fc
package
es
.
uvigo
.
esei
.
daa
.
rest
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
javax.ws.rs.DELETE
;
import
javax.ws.rs.FormParam
;
import
javax.ws.rs.GET
;
import
javax.ws.rs.POST
;
import
javax.ws.rs.PUT
;
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.daa.dao.DAOException
;
import
es.uvigo.esei.daa.dao.PetsDAO
;
import
es.uvigo.esei.daa.entities.Pet
;
/**
* REST resource for managing pets.
*
* @author Noelia García Hervella
*/
@Path
(
"/pets"
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
class
PetsResource
{
private
final
static
Logger
LOG
=
Logger
.
getLogger
(
PetsResource
.
class
.
getName
());
private
final
PetsDAO
dao
;
/**
* Constructs a new instance of {@link PetsResource}.
*/
public
PetsResource
()
{
this
(
new
PetsDAO
());
}
// Needed for testing purposes
PetsResource
(
PetsDAO
dao
)
{
this
.
dao
=
dao
;
}
/**
* Returns the complete list of pets stored in the system.
*
* @return a 200 OK response with the complete list of pets stored in the
* system. If an error happens while retrieving the list, a 500 Internal
* Server Error response with an error message will be returned.
*/
@GET
public
Response
list
(
@QueryParam
(
"owner"
)
int
id
)
{
try
{
return
Response
.
ok
(
this
.
dao
.
list
(
id
)).
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error listing pets"
,
e
);
return
Response
.
serverError
().
entity
(
e
.
getMessage
()).
build
();
}
}
/**
* Deletes a pet from the system.
*
* @param id the identifier of the pet to be deleted.
* @return a 200 OK response with the identifier of the pet that has
* been deleted. 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.
*/
@DELETE
@Path
(
"/{id}"
)
public
Response
delete
(
@PathParam
(
"id"
)
int
id
)
{
try
{
this
.
dao
.
delete
(
id
);
return
Response
.
ok
(
id
).
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid pet id in delete method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error deleting a pet"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
/**
* Creates a new pet in the system.
*
* @param name the name of the new pet.
* @param surname the surname of the new pet.
* @return a 200 OK response with a pet that has been created. If the
* name or the surname are not provided, 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.
*/
@POST
public
Response
add
(
@FormParam
(
"name"
)
String
name
,
@FormParam
(
"kind"
)
String
kind
,
@FormParam
(
"owner"
)
String
owner
)
{
try
{
final
Pet
newPet
=
this
.
dao
.
add
(
name
,
kind
,
owner
);
return
Response
.
ok
(
newPet
).
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid pet id in add method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error adding a pet"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
/**
* Modifies the data of a pet.
*
* @param id identifier of the pet to modify.
* @param name the new name of the pet.
* @param surname the new surname of the pet.
* @param owner identifier of the owner.
* @return a 200 OK response with a pet that has been modified. If the
* identifier does not corresponds with any user or the name or surname are
* not provided, 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.
*
*/
@PUT
@Path
(
"/{id}"
)
public
Response
modify
(
@PathParam
(
"id"
)
int
id
,
@FormParam
(
"name"
)
String
name
,
@FormParam
(
"kind"
)
String
kind
,
@FormParam
(
"owner"
)
String
owner
)
{
try
{
final
Pet
modifiedPet
=
new
Pet
(
id
,
name
,
kind
,
Integer
.
parseInt
(
owner
));
this
.
dao
.
modify
(
modifiedPet
);
return
Response
.
ok
(
modifiedPet
).
build
();
}
catch
(
NullPointerException
npe
)
{
final
String
message
=
String
.
format
(
"Invalid data for pet (name: %s, KIND: %s)"
,
name
,
kind
);
LOG
.
log
(
Level
.
FINE
,
message
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
message
)
.
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid pet id in modify method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error modifying a pet"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
}
target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst
View file @
4dfb13fc
es\uvigo\esei\daa\rest\PeopleResource.class
es\uvigo\esei\daa\rest\PeopleResource.class
es\uvigo\esei\daa\dao\PeopleDAO.class
es\uvigo\esei\daa\dao\PeopleDAO.class
es\uvigo\esei\daa\rest\PetsResource.class
es\uvigo\esei\daa\dao\PetsDAO.class
es\uvigo\esei\daa\entities\Pet.class
es\uvigo\esei\daa\entities\User.class
es\uvigo\esei\daa\entities\User.class
es\uvigo\esei\daa\dao\UsersDAO.class
es\uvigo\esei\daa\dao\UsersDAO.class
es\uvigo\esei\daa\dao\DAOException.class
es\uvigo\esei\daa\dao\DAOException.class
...
...
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