-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9e63d7
commit a76ddec
Showing
12 changed files
with
411 additions
and
42 deletions.
There are no files selected for viewing
43 changes: 31 additions & 12 deletions
43
...kend/src/main/java/com/br/emanuelap/housinglocationprojectbackend/services/AdService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,50 @@ | ||
package com.br.emanuelap.housinglocationprojectbackend.services; | ||
|
||
import com.br.emanuelap.housinglocationprojectbackend.entities.Ad; | ||
import com.br.emanuelap.housinglocationprojectbackend.exceptions.AppException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
import com.br.emanuelap.housinglocationprojectbackend.repositories.AdRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class AdService { | ||
|
||
@Autowired | ||
private AdRepository adRepository; | ||
// public List<Ad> findAll() {} | ||
public class AdService implements CrudInterface<Ad> { | ||
|
||
private final AdRepository adRepository; | ||
|
||
|
||
@Override | ||
public Ad save(Ad ad) { | ||
// | ||
// Optional<Ad> optionalAd = adRepository.findByTitle(ad.getTitle()); | ||
// if (optionalAd.isPresent()) { | ||
// throw new AppException("Add with same tittle already exist", HttpStatus.CONFLICT); | ||
// } | ||
// | ||
|
||
Optional<Ad> optionalAd = adRepository.findByTitle(ad.getTitle()); | ||
if (optionalAd.isPresent()) { | ||
throw new AppException("Add with same tittle already exist", HttpStatus.CONFLICT); | ||
} | ||
|
||
return adRepository.save(ad); | ||
} | ||
|
||
@Override | ||
public List<Ad> findAll() { | ||
return List.of(); | ||
} | ||
|
||
@Override | ||
public Optional<Ad> findById(Long id) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Ad update(Object obj) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void deleteById(Long id) { | ||
|
||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
.../src/main/java/com/br/emanuelap/housinglocationprojectbackend/services/CrudInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.br.emanuelap.housinglocationprojectbackend.services; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface CrudInterface<T> { | ||
|
||
T save(T obj); | ||
|
||
List<T> findAll(); | ||
|
||
Optional<T> findById(Long id); | ||
|
||
T update(Object obj); | ||
|
||
void deleteById(Long id); | ||
|
||
} |
5 changes: 1 addition & 4 deletions
5
...tbackend/services/AdServiceInterface.java → ...nprojectbackend/services/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
package com.br.emanuelap.housinglocationprojectbackend.services; | ||
|
||
public interface AdServiceInterface { | ||
|
||
|
||
|
||
public class UserService { | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { Routes } from '@angular/router'; | ||
import { HomeComponent } from './home/home.component'; | ||
import { DetailsComponent } from './details/details.component'; | ||
import { LoginComponent } from './login/login.component'; | ||
|
||
export const routes: Routes = [ | ||
{ path: '', component: LoginComponent, title: 'Login Page' }, | ||
{ path: '', component: HomeComponent, title: 'Home Page' }, | ||
{ path: 'details/:id', component: DetailsComponent, title: 'Home Details' }, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { AxiosService } from './axios.service'; | ||
|
||
describe('AxiosService', () => { | ||
let service: AxiosService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(AxiosService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Injectable } from '@angular/core'; | ||
import axios from 'axios'; | ||
// import * as jwt from 'jsonwebtoken'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AxiosService { | ||
constructor() { | ||
// axios.defaults.baseURL = 'http://localhost:8080'; | ||
// axios.defaults.headers.post['Content-Type'] = 'application/json'; | ||
} | ||
|
||
getAuthToken(): string | null { | ||
return window.localStorage.getItem('auth_token'); | ||
} | ||
|
||
getRole(): { username: string; role: string } | null { | ||
const token = this.getAuthToken(); | ||
if (token) { | ||
const payload = token.split('.')[1]; | ||
const decodedPayload = JSON.parse(atob(payload)); | ||
return { | ||
username: decodedPayload.nome, | ||
role: decodedPayload.role, | ||
}; | ||
} | ||
return null; | ||
} | ||
|
||
setAuthToken(token: string | null): void { | ||
if (token !== null) { | ||
window.localStorage.setItem('auth_token', token); | ||
} else { | ||
window.localStorage.removeItem('auth_token'); | ||
} | ||
} | ||
|
||
isAuthenticated(): boolean { | ||
const authToken = this.getAuthToken(); // Chame a função para obter o token JWT | ||
return !!authToken; // Verifique se authToken existe e não é nulo | ||
} | ||
|
||
logout(): void { | ||
this.setAuthToken(null); | ||
} | ||
|
||
request(method: string, url: string, data: any): Promise<any> { | ||
let headers: any = {}; | ||
|
||
if (this.getAuthToken() !== null) { | ||
headers = { Authorization: 'Bearer ' + this.getAuthToken() }; | ||
console.log('Token:', this.getAuthToken()); // Adicione esta linha para imprimir o token | ||
} | ||
|
||
return axios({ | ||
method: method, | ||
url: url, | ||
data: data, | ||
headers: headers, | ||
}); | ||
} | ||
} |
Oops, something went wrong.