Skip to content

Commit

Permalink
llogin com axios ok
Browse files Browse the repository at this point in the history
  • Loading branch information
Emanuel3queijos committed Apr 29, 2024
1 parent d9e63d7 commit a76ddec
Show file tree
Hide file tree
Showing 12 changed files with 411 additions and 42 deletions.
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) {

}
}
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);

}
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 {
}
56 changes: 53 additions & 3 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@fortawesome/free-solid-svg-icons": "^6.4.2",
"@ng-bootstrap/ng-bootstrap": "^16.0.0",
"@popperjs/core": "^2.11.8",
"axios": "^1.6.8",
"bootstrap": "^5.3.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { RouterModule } from '@angular/router';
standalone: true,
imports: [RouterOutlet, HomeComponent, MatToolbarModule, RouterModule],
template: `
<main>
<main >
<a [routerLink]="['/']">
<mat-toolbar color="primary">
<!-- fazer comnponent de navbar para comportar as coisas -->
<header class="brand-name">
<img
class="brand-logo"
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/app.routes.ts
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' },
];
16 changes: 16 additions & 0 deletions frontend/src/app/axios.service.spec.ts
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();
});
});
63 changes: 63 additions & 0 deletions frontend/src/app/axios.service.ts
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,
});
}
}
Loading

0 comments on commit a76ddec

Please sign in to comment.