API Rewrite

This commit is contained in:
Drake Marino 2024-06-13 14:27:36 -05:00
parent 0a1250dfd2
commit 9116876f7b
3 changed files with 136 additions and 36 deletions

View File

@ -3,6 +3,7 @@ version: '3'
services: services:
fbla_api: fbla_api:
image: fbla-api image: fbla-api
restart: unless-stopped
ports: ports:
- "8000:8000" - "8000:8000"
volumes: volumes:

View File

@ -22,13 +22,12 @@ enum BusinessType {
other, other,
} }
enum JobType { cashier, server, mechanic } enum JobType { cashier, server, mechanic, other }
class Business { class Business {
int id; int id;
String name; String name;
String description; String description;
BusinessType? type;
String? website; String? website;
String? contactName; String? contactName;
String? contactEmail; String? contactEmail;
@ -41,7 +40,6 @@ class Business {
{required this.id, {required this.id,
required this.name, required this.name,
required this.description, required this.description,
this.type,
this.website, this.website,
this.contactName, this.contactName,
this.contactEmail, this.contactEmail,
@ -51,20 +49,10 @@ class Business {
this.locationAddress}); this.locationAddress});
factory Business.fromJson(Map<String, dynamic> json) { factory Business.fromJson(Map<String, dynamic> json) {
bool typeValid = true;
try {
BusinessType.values.byName(json['type']);
} catch (e) {
typeValid = false;
}
return Business( return Business(
id: json['id'], id: json['id'],
name: json['name'], name: json['name'],
description: json['description'], description: json['description'],
type: typeValid
? BusinessType.values.byName(json['type'])
: BusinessType.other,
website: json['website'], website: json['website'],
contactName: json['contactName'], contactName: json['contactName'],
contactEmail: json['contactEmail'], contactEmail: json['contactEmail'],
@ -77,19 +65,41 @@ class Business {
} }
class JobListing { class JobListing {
String? id;
String? businessId;
String name; String name;
String description; String description;
JobType type; JobType type;
String wage; String? wage;
String link; String? link;
JobListing({ JobListing(
required this.name, {this.id,
required this.description, this.businessId,
required this.type, required this.name,
required this.wage, required this.description,
required this.link, required this.type,
}); this.wage,
this.link});
factory JobListing.fromJson(Map<String, dynamic> json) {
bool typeValid = true;
try {
JobType.values.byName(json['type']);
} catch (e) {
typeValid = false;
}
return JobListing(
id: json['id'],
businessId: json['businessId'],
name: json['name'],
description: json['description'],
type: typeValid ? JobType.values.byName(json['type']) : JobType.other,
wage: json['wage'],
link: json['link'],
);
}
} }
Future<String> fetchBusinessData() async { Future<String> fetchBusinessData() async {
@ -156,9 +166,13 @@ void main() async {
json_build_object( json_build_object(
'id', id, 'id', id,
'name', name, 'name', name,
'description', description 'description', description,
'website', website,
'contactEmail', "contactEmail",
'contactPhone', "contactPhone",
'locationName', "locationName"
) )
) FROM public.businesses WHERE id IN (SELECT id FROM public.listings WHERE type='${filters.elementAt(i)}') ) FROM public.businesses WHERE id IN (SELECT "businessId" FROM public.listings WHERE type='${filters.elementAt(i)}')
'''))[0][0]; '''))[0][0];
if (postgresResult != null) { if (postgresResult != null) {
@ -184,7 +198,6 @@ void main() async {
'id', b.id, 'id', b.id,
'name', b.name, 'name', b.name,
'description', b.description, 'description', b.description,
'type', b.type,
'website', b.website, 'website', b.website,
'contactName', b."contactName", 'contactName', b."contactName",
'contactEmail', b."contactEmail", 'contactEmail', b."contactEmail",
@ -254,8 +267,8 @@ void main() async {
Business business = Business.fromJson(json); Business business = Business.fromJson(json);
await postgres.query(''' await postgres.query('''
INSERT INTO businesses (name, description, type, website, "contactName", "contactPhone", "contactEmail", notes, "locationName", "locationAddress") INSERT INTO businesses (name, description, website, "contactName", "contactPhone", "contactEmail", notes, "locationName", "locationAddress")
VALUES ('${business.name.replaceAll("'", "''")}', '${business.description.replaceAll("'", "''")}', '${business.type!.name}', '${business.website!}', '${business.contactName!.replaceAll("'", "''")}', '${business.contactPhone!}', '${business.contactEmail!}', '${business.notes!.replaceAll("'", "''")}', '${business.locationName!.replaceAll("'", "''")}', '${business.locationAddress!.replaceAll("'", "''")}') VALUES ('${business.name.replaceAll("'", "''")}', '${business.description.replaceAll("'", "''")}', '${business.website ?? 'NULL'}', '${business.contactName?.replaceAll("'", "''") ?? 'NULL'}', '${business.contactPhone ?? 'NULL'}', '${business.contactEmail ?? 'NULL'}', '${business.notes?.replaceAll("'", "''") ?? 'NULL'}', '${business.locationName?.replaceAll("'", "''") ?? 'NULL'}', '${business.locationAddress?.replaceAll("'", "''") ?? 'NULL'}')
'''); ''');
final dbBusiness = await postgres.query('''SELECT * FROM public.businesses final dbBusiness = await postgres.query('''SELECT * FROM public.businesses
@ -283,6 +296,40 @@ void main() async {
headers: {'Access-Control-Allow-Origin': '*'}, headers: {'Access-Control-Allow-Origin': '*'},
); );
}); });
app.post('/fbla-api/createlisting', (Request request) async {
print('create business request received');
final payload = await request.readAsString();
var auth = request.headers['Authorization']?.replaceAll('Bearer ', '');
try {
JWT.verify(auth!, secretKey);
var json = jsonDecode(payload);
JobListing listing = JobListing.fromJson(json);
await postgres.query('''
INSERT INTO listings ("businessId", name, description, type, wage, link)
VALUES ('${listing.businessId}' '${listing.name.replaceAll("'", "''")}', '${listing.description.replaceAll("'", "''")}', '${listing.type.name}', '${listing.wage ?? 'NULL'}', '${listing.link?.replaceAll("'", "''") ?? 'NULL'}')
''');
final dbListing = await postgres.query('''SELECT id FROM public.listings
ORDER BY id DESC LIMIT 1''');
var id = dbListing[0][0];
return Response.ok(
id.toString(),
headers: {'Access-Control-Allow-Origin': '*'},
);
} on JWTExpiredException {
print('JWT Expired');
} on JWTException catch (e) {
print(e.message);
}
return Response.unauthorized(
'unauthorized',
headers: {'Access-Control-Allow-Origin': '*'},
);
});
app.post('/fbla-api/deletebusiness', (Request request) async { app.post('/fbla-api/deletebusiness', (Request request) async {
print('delete business request received'); print('delete business request received');
@ -293,11 +340,7 @@ void main() async {
var json = jsonDecode(payload); var json = jsonDecode(payload);
var id = json['id']; var id = json['id'];
await postgres.query(''' await postgres.query('DELETE FROM public.business WHERE id=$id;');
DELETE FROM public.businesses
WHERE id IN
($id);
''');
try { try {
await File('logos/$id.png').delete(); await File('logos/$id.png').delete();
@ -320,6 +363,33 @@ void main() async {
headers: {'Access-Control-Allow-Origin': '*'}, headers: {'Access-Control-Allow-Origin': '*'},
); );
}); });
app.post('/fbla-api/deletelisting', (Request request) async {
print('delete listing request received');
final payload = await request.readAsString();
var auth = request.headers['Authorization']?.replaceAll('Bearer ', '');
try {
JWT.verify(auth!, secretKey);
var json = jsonDecode(payload);
var id = json['id'];
await postgres.query('DELETE FROM public.listings WHERE id=$id;');
return Response.ok(
id.toString(),
headers: {'Access-Control-Allow-Origin': '*'},
);
} on JWTExpiredException {
print('JWT Expired');
} on JWTException catch (e) {
print(e.message);
}
return Response.unauthorized(
'unauthorized',
headers: {'Access-Control-Allow-Origin': '*'},
);
});
app.post('/fbla-api/editbusiness', (Request request) async { app.post('/fbla-api/editbusiness', (Request request) async {
print('edit business request received'); print('edit business request received');
@ -333,7 +403,7 @@ void main() async {
await postgres.query(''' await postgres.query('''
UPDATE businesses SET UPDATE businesses SET
name = '${business.name.replaceAll("'", "''").replaceAll("\"", "\"\"")}'::text, description = '${business.description.replaceAll("'", "''").replaceAll("\"", "\"\"")}'::text, website = '${business.website!}'::text, type = '${business.type!.name}'::text, "contactName" = '${business.contactName!.replaceAll("'", "''").replaceAll("\"", "\"\"")}'::text, "contactPhone" = '${business.contactPhone!}'::text, "contactEmail" = '${business.contactEmail!}'::text, notes = '${business.notes!.replaceAll("'", "''").replaceAll("\"", "\"\"")}'::text, "locationName" = '${business.locationName!.replaceAll("'", "''").replaceAll("\"", "\"\"")}'::text, "locationAddress" = '${business.locationAddress!.replaceAll("'", "''").replaceAll("\"", "\"\"")}'::text WHERE name = '${business.name.replaceAll("'", "''").replaceAll("\"", "\"\"")}'::text, description = '${business.description.replaceAll("'", "''").replaceAll("\"", "\"\"")}'::text, website = '${business.website!}'::text, "contactName" = '${business.contactName!.replaceAll("'", "''").replaceAll("\"", "\"\"")}'::text, "contactPhone" = '${business.contactPhone!}'::text, "contactEmail" = '${business.contactEmail!}'::text, notes = '${business.notes!.replaceAll("'", "''").replaceAll("\"", "\"\"")}'::text, "locationName" = '${business.locationName!.replaceAll("'", "''").replaceAll("\"", "\"\"")}'::text, "locationAddress" = '${business.locationAddress!.replaceAll("'", "''").replaceAll("\"", "\"\"")}'::text WHERE
id = ${business.id}; id = ${business.id};
'''); ''');
@ -366,6 +436,38 @@ void main() async {
headers: {'Access-Control-Allow-Origin': '*'}, headers: {'Access-Control-Allow-Origin': '*'},
); );
}); });
app.post('/fbla-api/editlisting', (Request request) async {
print('edit listing request received');
final payload = await request.readAsString();
var auth = request.headers['Authorization']?.replaceAll('Bearer ', '');
try {
JWT.verify(auth!, secretKey);
var json = jsonDecode(payload);
JobListing listing = JobListing.fromJson(json);
await postgres.query('''
UPDATE listings SET
"businessId" = ${listing.businessId}, name = '${listing.name.replaceAll("'", "''")}'::text, description = '${listing.description.replaceAll("'", "''")}'::text, type = '${listing.type.name}'::text, wage = '${listing.wage ?? 'NULL'}'::text, link = '${listing.link?.replaceAll("'", "''") ?? 'NULL'}'::text WHERE
id = ${listing.id};
''');
return Response.ok(
listing.id.toString(),
headers: {'Access-Control-Allow-Origin': '*'},
);
} on JWTExpiredException {
print('JWT Expired');
} on JWTException catch (e) {
print(e.message);
}
return Response.unauthorized(
'unauthorized',
headers: {'Access-Control-Allow-Origin': '*'},
);
});
app.post('/fbla-api/signin', (Request request) async { app.post('/fbla-api/signin', (Request request) async {
print('signin request received'); print('signin request received');
@ -541,6 +643,4 @@ void main() async {
final server = await io.serve(app, _hostname, _port); final server = await io.serve(app, _hostname, _port);
print('Serving at http://${server.address.host}:${server.port}'); print('Serving at http://${server.address.host}:${server.port}');
// print((await postgres.query('select testdouble from public.test')));
} }

View File

@ -81,7 +81,6 @@ void main() async {
"id": 0, "id": 0,
"name": "tmp", "name": "tmp",
"description": "tmp", "description": "tmp",
"type": "business",
"website": "tmp", "website": "tmp",
"contactName": "tmp", "contactName": "tmp",
"contactEmail": "tmp", "contactEmail": "tmp",