Top Donators / Total
This commit is contained in:
@@ -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:
|
||||
|
||||
BIN
api/db.sqlite
BIN
api/db.sqlite
Binary file not shown.
50
api/serv.py
50
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 += '{} {} <br/>'.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 += '{} {} <br/>'.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():
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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']
|
||||
})
|
||||
|
||||
@@ -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()]
|
||||
};
|
||||
|
||||
@@ -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 },
|
||||
];
|
||||
|
||||
@@ -8,3 +8,8 @@ export class WSMessage {
|
||||
Name : String;
|
||||
Euro : number;
|
||||
}
|
||||
|
||||
export class Donator {
|
||||
name : String;
|
||||
amount : number;
|
||||
}
|
||||
|
||||
@@ -1 +1,12 @@
|
||||
<p>topdonors works!</p>
|
||||
<table>
|
||||
<thead>
|
||||
<th>Nom</th>
|
||||
<th>Montant</th>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr *ngFor="let d of donators" >
|
||||
<td>{{d.name}}</td>
|
||||
<td>{{d.amount}}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
1
frontend/src/app/total/total.component.html
Normal file
1
frontend/src/app/total/total.component.html
Normal file
@@ -0,0 +1 @@
|
||||
<p>{{Total}}</p>
|
||||
0
frontend/src/app/total/total.component.scss
Normal file
0
frontend/src/app/total/total.component.scss
Normal file
23
frontend/src/app/total/total.component.spec.ts
Normal file
23
frontend/src/app/total/total.component.spec.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { TotalComponent } from './total.component';
|
||||
|
||||
describe('TotalComponent', () => {
|
||||
let component: TotalComponent;
|
||||
let fixture: ComponentFixture<TotalComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [TotalComponent]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(TotalComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
29
frontend/src/app/total/total.component.ts
Normal file
29
frontend/src/app/total/total.component.ts
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
4
frontend/src/environments/environment.development.ts
Normal file
4
frontend/src/environments/environment.development.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export const environment = {
|
||||
production : false,
|
||||
apiUrl : 'http://localhost:5000/'
|
||||
};
|
||||
4
frontend/src/environments/environment.ts
Normal file
4
frontend/src/environments/environment.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export const environment = {
|
||||
production : false,
|
||||
apiUrl : 'http://localhost:5000/'
|
||||
};
|
||||
22
frontend/src/services/api.ts
Normal file
22
frontend/src/services/api.ts
Normal file
@@ -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<Donator[]> {
|
||||
return this.httpClient.get<Donator[]>(`${environment.apiUrl}/topdonators`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user