diff --git a/api/classes.py b/api/classes.py index 4691fa4..1ad78a3 100644 --- a/api/classes.py +++ b/api/classes.py @@ -8,8 +8,39 @@ class CustomJSONProvider(DefaultJSONProvider): def default(obj) -> dict : if isinstance(obj,Payment): return obj.to_json() + + if isinstance(obj,Donator): + return obj.to_json() return DefaultJSONProvider.default(obj) + +class Donator(json.JSONEncoder): + name : str + amount : float + + @staticmethod + def top_donator(conn: Connection): + cur = conn.cursor() + cur.execute("""select name, sum(amount) from orders + group by name + LIMIT 5 + """) + data = cur.fetchall(); + print(data); + if data is not None : + return [Donator(p[0],p[1]) for p in data] + else: + return None + + def __init__(self,name,amount) -> None: + self.name = name + self.amount = float(amount/100) + + def to_json(self) -> dict : + return { "name" : self.name, + "amount": self.amount + } + class Payment(json.JSONEncoder): id : int amount: float @@ -46,6 +77,7 @@ class Payment(json.JSONEncoder): return [Payment(p[0],p[1],p[2],p[3]) for p in data] return None + def __repr__(self) -> str: return '{} - {}€- {}'.format(self.name,self.amount/100,self.message) def to_json(self) -> dict: diff --git a/api/db.sqlite b/api/db.sqlite index 64120f8..43252af 100644 Binary files a/api/db.sqlite and b/api/db.sqlite differ diff --git a/api/serv.py b/api/serv.py index 30efcae..ff55c37 100755 --- a/api/serv.py +++ b/api/serv.py @@ -1,11 +1,12 @@ #!/bin/env python3 import os +from re import A import sqlite3 from flask import Flask, make_response, render_template,request,g,jsonify from flask_sock import Sock -from classes import Payment, Client, CustomJSONProvider - - +from classes import Payment, Client,Donator, CustomJSONProvider +import random +from flask_cors import CORS,cross_origin @@ -18,6 +19,8 @@ app.config.from_mapping( ) app.json = CustomJSONProvider(app) sock = Sock(app) +cors = CORS(app) +app.config['CORS_HEADERS'] = 'Content-Type' def get_db(): @@ -43,20 +46,21 @@ def notify_client_payment(msg): def index(): return render_template('index.html') -@app.route('/show') -def show(): - db = get_db() - cur = db.cursor() - cur.execute('select * from orders;') - info = cur.fetchall(); - val = "" - for i in info: - val += '{} {}
'.format(i[0],i[1]) - return make_response(val, 200) +#@app.route('/show') +#def show(): +# db = get_db() +# cur = db.cursor() +# cur.execute('select * from orders;') +# info = cur.fetchall(); +# val = "" +# for i in info: +# val += '{} {}
'.format(i[0],i[1]) +# return make_response(val, 200) @app.route('/test') def test(): - p = Payment(1,5000,'TEST DE MESSAGE BIEN LONG SKLDJQLKDJQLKSJDQLSKJDQLKJD','Nom donation') + p = Payment(int(random.random()*100),5000,'TEST DE MESSAGE BIEN LONG SKLDJQLKDJQLKSJDQLSKJDQLKJD','Nom donation') + p.save(get_db()) notify_client_payment(p) return jsonify(p), 200 @@ -85,6 +89,24 @@ def notify(sock): data = sock.receive() sock.send(data) +@app.route('/total') +@cross_origin() +def total(): + data= Payment.get_all(get_db()) + if data is not None: + total = sum(x.amount for x in data) + return jsonify(total), 200 + else: + return jsonify(0); + +@app.route('/topdonators') +@cross_origin() +def topdonator(): + data = Donator.top_donator(get_db()) + if data is not None: + return jsonify(data), 200 + else: + return jsonify(0); @app.route('/notifications',methods=['POST']) def notifications(): diff --git a/frontend/angular.json b/frontend/angular.json index d04ae21..e909bb3 100644 --- a/frontend/angular.json +++ b/frontend/angular.json @@ -53,7 +53,13 @@ "development": { "optimization": false, "extractLicenses": false, - "sourceMap": true + "sourceMap": true, + "fileReplacements": [ + { + "replace": "src/environments/environment.ts", + "with": "src/environments/environment.development.ts" + } + ] } }, "defaultConfiguration": "production" diff --git a/frontend/src/app/app.component.ts b/frontend/src/app/app.component.ts index 3622117..b886d65 100644 --- a/frontend/src/app/app.component.ts +++ b/frontend/src/app/app.component.ts @@ -1,11 +1,12 @@ import { Component } from '@angular/core'; import { CommonModule } from '@angular/common'; import { RouterOutlet } from '@angular/router'; +import {HttpClientModule} from '@angular/common/http'; @Component({ selector: 'app-root', standalone: true, - imports: [CommonModule, RouterOutlet], + imports: [CommonModule, RouterOutlet,HttpClientModule], templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) diff --git a/frontend/src/app/app.config.ts b/frontend/src/app/app.config.ts index 6c6ef60..db1a923 100644 --- a/frontend/src/app/app.config.ts +++ b/frontend/src/app/app.config.ts @@ -1,8 +1,9 @@ +import {provideHttpClient} from '@angular/common/http'; import { ApplicationConfig } from '@angular/core'; import { provideRouter } from '@angular/router'; import { routes } from './app.routes'; export const appConfig: ApplicationConfig = { - providers: [provideRouter(routes)] + providers: [provideRouter(routes),provideHttpClient()] }; diff --git a/frontend/src/app/app.routes.ts b/frontend/src/app/app.routes.ts index 78f471f..1801f92 100644 --- a/frontend/src/app/app.routes.ts +++ b/frontend/src/app/app.routes.ts @@ -1,9 +1,10 @@ import {DonationComponent} from "./donation/donation.component"; import {TopdonorsComponent} from "./topdonors/topdonors.component"; import { Routes } from '@angular/router'; +import {TotalComponent} from "./total/total.component"; export const routes: Routes = [ { path: 'topdonors' , component: TopdonorsComponent }, - { path: 'donation' , component: DonationComponent } - + { path: 'donations' , component: DonationComponent }, + { path: 'total' , component: TotalComponent }, ]; diff --git a/frontend/src/app/models/WSMessage.ts b/frontend/src/app/models/WSMessage.ts index b9a838d..f895260 100644 --- a/frontend/src/app/models/WSMessage.ts +++ b/frontend/src/app/models/WSMessage.ts @@ -8,3 +8,8 @@ export class WSMessage { Name : String; Euro : number; } + +export class Donator { + name : String; + amount : number; +} diff --git a/frontend/src/app/topdonors/topdonors.component.html b/frontend/src/app/topdonors/topdonors.component.html index 2e01d02..54de94f 100644 --- a/frontend/src/app/topdonors/topdonors.component.html +++ b/frontend/src/app/topdonors/topdonors.component.html @@ -1 +1,12 @@ -

topdonors works!

+ + + + + + + + + + + +
NomMontant
{{d.name}}{{d.amount}}
diff --git a/frontend/src/app/topdonors/topdonors.component.ts b/frontend/src/app/topdonors/topdonors.component.ts index b8ddb11..8e78a45 100644 --- a/frontend/src/app/topdonors/topdonors.component.ts +++ b/frontend/src/app/topdonors/topdonors.component.ts @@ -1,5 +1,8 @@ -import { Component } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import { CommonModule } from '@angular/common'; +import {WebsocketService} from '../../services/websocket'; +import {ApiService} from '../../services/api'; +import {Donator} from '../models/WSMessage'; @Component({ selector: 'app-topdonors', @@ -8,6 +11,20 @@ import { CommonModule } from '@angular/common'; templateUrl: './topdonors.component.html', styleUrl: './topdonors.component.scss' }) -export class TopdonorsComponent { +export class TopdonorsComponent implements OnInit{ + donators: Donator[] = [] + + constructor( private socket : WebsocketService,private ApiService: ApiService) {}; + + ngOnInit(): void { + this.getDonators(); + this.socket.Messages.subscribe(s => { + this.getDonators(); + }); + } + + getDonators(){ + this.ApiService.TopDonators().subscribe(donators => this.donators = donators); + } } diff --git a/frontend/src/app/total/total.component.html b/frontend/src/app/total/total.component.html new file mode 100644 index 0000000..5d7b79d --- /dev/null +++ b/frontend/src/app/total/total.component.html @@ -0,0 +1 @@ +

{{Total}}

diff --git a/frontend/src/app/total/total.component.scss b/frontend/src/app/total/total.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/frontend/src/app/total/total.component.spec.ts b/frontend/src/app/total/total.component.spec.ts new file mode 100644 index 0000000..aa92486 --- /dev/null +++ b/frontend/src/app/total/total.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { TotalComponent } from './total.component'; + +describe('TotalComponent', () => { + let component: TotalComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [TotalComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(TotalComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/total/total.component.ts b/frontend/src/app/total/total.component.ts new file mode 100644 index 0000000..f6b7272 --- /dev/null +++ b/frontend/src/app/total/total.component.ts @@ -0,0 +1,29 @@ +import { Component, OnInit } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import {WebsocketService} from '../../services/websocket'; +import {ApiService} from '../../services/api'; + +@Component({ + selector: 'app-total', + standalone: true, + imports: [CommonModule], + templateUrl: './total.component.html', + styleUrl: './total.component.scss' +}) +export class TotalComponent implements OnInit{ + + Total : string; + + constructor( private socket : WebsocketService,private ApiService: ApiService) {}; + + ngOnInit(): void { + this.getTotal(); + this.socket.Messages.subscribe(s => { + this.getTotal(); + }); + } + + getTotal(){ + this.ApiService.Total().subscribe(t => this.Total = t as string); + } +} diff --git a/frontend/src/environments/environment.development.ts b/frontend/src/environments/environment.development.ts new file mode 100644 index 0000000..11ba5ec --- /dev/null +++ b/frontend/src/environments/environment.development.ts @@ -0,0 +1,4 @@ +export const environment = { + production : false, + apiUrl : 'http://localhost:5000/' +}; diff --git a/frontend/src/environments/environment.ts b/frontend/src/environments/environment.ts new file mode 100644 index 0000000..11ba5ec --- /dev/null +++ b/frontend/src/environments/environment.ts @@ -0,0 +1,4 @@ +export const environment = { + production : false, + apiUrl : 'http://localhost:5000/' +}; diff --git a/frontend/src/services/api.ts b/frontend/src/services/api.ts new file mode 100644 index 0000000..6862f1a --- /dev/null +++ b/frontend/src/services/api.ts @@ -0,0 +1,22 @@ + +import {HttpClient} from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import {Observable, Observer, Subject} from 'rxjs'; +import { Donator, WSMessage } from '../app/models/WSMessage'; +import {environment} from '../environments/environment'; +@Injectable({ + providedIn: 'root', +}) +export class ApiService { + + constructor(private httpClient : HttpClient) { + } + + Total() { + return this.httpClient.get(`${environment.apiUrl}/total`); + } + + TopDonators() : Observable { + return this.httpClient.get(`${environment.apiUrl}/topdonators`); + } +}