Skip to content

Commit

Permalink
fixed Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebas104 committed Apr 19, 2020
1 parent 039ddc6 commit 2f406b1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ <h1 class="mb-3 bread">Propiedad seleccionada</h1>
</div>
</section>


<section class="ftco-section ftco-property-details">
<div class="container">
<div class="row justify-content-center">
Expand Down Expand Up @@ -88,9 +89,9 @@ <h2>{{advertisement.property}} en {{advertisement.location}}</h2>
{{advertisement.bathrooms}} baños amplios.</p>
</div>
<div class="tab-pane fade" id="pills-review" role="tabpanel" aria-labelledby="pills-review-tab">
<!-- <div *ngIf='isLogged'>
<div *ngIf="isLogged">
<commentMessage></commentMessage>
</div> -->
</div>
<p> Si quiere leer las opiniones o poner una reseña tiene que registrarse. </p>
<p>Si desea resgistrarse ves a la pestaña "Home" y clicka arriba a la derecha en "Acceso Usuario". </p>
</div>
Expand All @@ -99,4 +100,6 @@ <h2>{{advertisement.property}} en {{advertisement.location}}</h2>
</div>
</div>
</div>


</section>
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ import { Advertisements } from '../entity/advertisement';
export class AdvertisementSingleComponent{

advertisement: Advertisements;
isLogged: boolean = false;

constructor(
private router: Router,
private service: AdvertisementService, activatedRoute: ActivatedRoute) {
private service: AdvertisementService, activatedRoute: ActivatedRoute) {
if ( localStorage.getItem('role') != null ) {
if (localStorage.getItem('role').localeCompare('ROLE_ADMIN') == 0 || localStorage.getItem('role').localeCompare('ROLE_USER') == 0) {
this.isLogged = true;
}
}
let id = activatedRoute.snapshot.params['id'];
console.log(this.isLogged);
this.getAdvertisement(id);
console.log(this.advertisement);
}

// ngOnInit() {
Expand All @@ -30,12 +38,11 @@ export class AdvertisementSingleComponent{
// error => console.log(error)
// );
// }

getAdvertisement(id: number) {
this.service.getAdvertisement(id).subscribe(
advertisement => this.advertisement = advertisement,
error => console.log(error)
);
}
}

}
14 changes: 8 additions & 6 deletions WebApp12Angular/src/app/comment.component.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

<div >
<textarea [(ngModel)]="comment"></textarea>
<textarea [(ngModel)]="message"></textarea>
<br>
<br>
<button (click)="post()">Comment</button>
<div *ngFor= "let comments of postComment">
<p>{{comment}}</p>

<button (click)="uploadComment(comment.id)">Comment</button>
<div *ngFor= "let comment of comments">
<p>{{comment.author}}</p>
<p>{{comment.message}}</p>
<a class="btn d-flex align-items-center btn-custom-edit" id="deleteMethod" style="float:left;" (click)="deleteComment(comment.id)">
<span class="icon-trash" ></span>
</a>
</div>
</div>
30 changes: 25 additions & 5 deletions WebApp12Angular/src/app/comment.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ import {Comment} from './entity/comment';
})
export class commentComponent {

comment : Comment;

comments : Comment[] = [];
message= "";
postComment=[];
isLogged : boolean= false;
id: number;
activatedRoute: ActivatedRoute

constructor(private router: Router, activatedRoute: ActivatedRoute, private commentService: CommentService) {
let id = activatedRoute.snapshot.params['id'];
this.id =id;
this.getComments(id);
}

Expand All @@ -36,12 +39,29 @@ export class commentComponent {
}

getComments(id: number){
this.commentService.getComment(id).subscribe(
comment => this.comment = comment,
error => console.log(error)
this.commentService.getComments(id,0,50).subscribe(
comment => this.comments = comment,
error => console.log(error)
);

}

uploadComment(id: number){
let author= localStorage.getItem('name');
console.log("message= "+ this.message);
console.log("postcomment="+ this.postComment);
let comment = new Comment(author, this.message)
console.log(comment);
this.commentService.uploadComment(this.id, comment).subscribe(
() => this.router.navigate(['/advertisement',this.id]),
error => console.log(error)
);
}

deleteComment(idComment: number) {
this.commentService.deleteComment(this.id,idComment).subscribe(
() => window.location.reload(),
error => console.log(error)
);
}
}
25 changes: 17 additions & 8 deletions WebApp12Angular/src/app/service/comment.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpEvent } from '@angular/common/http';
import { HttpClient, HttpHeaders, HttpEvent, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { Comment } from '../entity/comment';
Expand All @@ -18,32 +18,41 @@ export class CommentService{
return Observable.throw('Server error (' + error.status + '): ' + error);
}

getComment(id: number): Observable<Comment>{
return this.http.get<Comment>(this.urlEndPoint + id +'/comments' )
getComments(id: number,page: number, quantity: number): Observable<Comment[]>{
return this.http.get<Comment[]>(this.urlEndPoint + id +'/comments'+ "?page="+page+"&number="+quantity)
.pipe(
map(response => response),
catchError(error => this.handleError(error))
);
}

uploadComment(comment: Comment): Observable<HttpEvent<{}>>{
uploadComment(id: number,comment: Comment): Observable<HttpEvent<{}>>{
const body = JSON.stringify(comment);
console.log(body);
let formData = new FormData();
formData.append("author", comment.author);
formData.append("message",comment.message);
return
const headers = new HttpHeaders({
'Content-Type': 'application/json',
});
const req = new HttpRequest('POST', this.urlEndPoint+id+"/comments", body, {headers,
reportProgress: true
});

return this.http.request(req);
}

editComment(comment: Comment){
return this.http.put(this.urlEndPoint + comment.id, comment)
.pipe(
catchError(error => this.handleError(error))
);
}
deleteComment(id: number){
return this.http.delete<Comment>(this.urlEndPoint +id)
deleteComment(id: number, idComment: number){
return this.http.delete<Comment>(this.urlEndPoint +id + "/comments/"+ idComment)
.pipe(
catchError(err =>this.handleError(err))
);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/advertisements/").permitAll();
http.authorizeRequests().antMatchers(HttpMethod.POST, "/api/advertisements/new").hasAnyRole("USER");
http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/advertisements/{id}/comments").permitAll();
http.authorizeRequests().antMatchers(HttpMethod.POST, "/api/advertisements/{id}/comments").hasAnyRole("USER");
http.authorizeRequests().antMatchers(HttpMethod.POST, "/api/advertisements/{id}/comments").hasAnyRole("USER","ADMIN");
http.authorizeRequests().antMatchers(HttpMethod.DELETE, "/api/advertisements/{id}/comments/{id}").hasAnyRole("USER","ADMIN");


Expand Down

0 comments on commit 2f406b1

Please sign in to comment.