0.2 fixes

This commit is contained in:
2024-06-23 14:36:18 -05:00
parent 4517ec3078
commit 03abc1191d
15 changed files with 1135 additions and 766 deletions
+8 -7
View File
@@ -1,14 +1,17 @@
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';
import 'package:argon2/argon2.dart';
import 'dart:io';
import 'package:postgres/postgres.dart';
// Set these to the desired username and password of your user
String username = 'admin';
String password = 'password';
String password = 'adminPassword';
var r = Random.secure();
String randomSalt = String.fromCharCodes(List.generate(32, (index) => r.nextInt(33) + 89));
String randomSalt =
String.fromCharCodes(List.generate(32, (index) => r.nextInt(33) + 89));
final salt = randomSalt.toBytesLatin1();
var parameters = Argon2Parameters(
@@ -37,10 +40,8 @@ Future<void> main() async {
argon2.generateBytes(passwordBytes, result);
var resultHex = result.toHexString();
postgres.query(
'''
postgres.query('''
INSERT INTO public.users (username, password_hash, salt)
VALUES ('$username', '$resultHex', '$randomSalt')
'''
);
''');
}
+51 -44
View File
@@ -24,6 +24,8 @@ enum BusinessType {
enum JobType { cashier, server, mechanic, other }
enum OfferType { job, internship, apprenticeship }
class Business {
int id;
String name;
@@ -84,6 +86,7 @@ class JobListing {
JobType type;
String? wage;
String? link;
OfferType offerType;
JobListing(
{this.id,
@@ -92,7 +95,8 @@ class JobListing {
required this.description,
required this.type,
this.wage,
this.link});
this.link,
required this.offerType});
factory JobListing.fromJson(Map<String, dynamic> json) {
bool typeValid = true;
@@ -103,14 +107,14 @@ class JobListing {
}
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'],
);
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'],
offerType: OfferType.values.byName(json['offerType']));
}
}
@@ -166,12 +170,15 @@ void main() async {
app.get('/fbla-api/businessdata/overview/jobs', (Request request) async {
print('business overview request received');
var filters = request.url.queryParameters['filters']?.split(',') ??
var typeFilters = request.url.queryParameters['typeFilters']?.split(',') ??
JobType.values.asNameMap().keys;
var offerFilters =
request.url.queryParameters['offerFilters']?.split(',') ??
OfferType.values.asNameMap().keys;
Map<String, dynamic> output = {};
for (int i = 0; i < filters.length; i++) {
for (int i = 0; i < typeFilters.length; i++) {
var postgresResult = (await postgres.query('''
SELECT json_agg(
json_build_object(
@@ -181,6 +188,7 @@ void main() async {
'contactEmail', b."contactEmail",
'contactPhone', b."contactPhone",
'locationName', b."locationName",
'locationAddress', b."locationAddress",
'listings', (
SELECT json_agg(
json_build_object(
@@ -188,22 +196,23 @@ void main() async {
'name', l.name,
'description', l.description,
'type', l.type,
'offerType', l."offerType",
'wage', l.wage,
'link', l.link
)
)
FROM listings l
WHERE l."businessId" = b.id AND l.type = '${filters.elementAt(i)}'
WHERE l."businessId" = b.id AND l.type = '${typeFilters.elementAt(i)}' AND l."offerType" IN (${offerFilters.map((element) => "'$element'").join(',')})
)
)
)
FROM businesses b
WHERE b.id IN (SELECT "businessId" FROM public.listings WHERE type='${filters.elementAt(i)}')
WHERE b.id IN (SELECT "businessId" FROM public.listings WHERE type='${typeFilters.elementAt(i)}' AND "offerType" IN (${offerFilters.map((element) => "'$element'").join(',')}))
GROUP BY b.id;
'''));
if (postgresResult.isNotEmpty) {
output.addAll({filters.elementAt(i): postgresResult[0][0]});
output.addAll({typeFilters.elementAt(i): postgresResult[0][0]});
}
}
@@ -234,7 +243,8 @@ void main() async {
'website', website,
'contactEmail', "contactEmail",
'contactPhone', "contactPhone",
'locationName', "locationName"
'locationName', "locationName",
'locationAddress', "locationAddress"
)
) FROM public.businesses WHERE type='${filters.elementAt(i)}'
'''))[0][0];
@@ -302,7 +312,8 @@ void main() async {
'description', l.description,
'type', l.type,
'wage', l.wage,
'link', l.link
'link', l.link,
'offerType', l."offerType"
)
)
END
@@ -348,25 +359,11 @@ void main() async {
'contactPhone', b."contactPhone",
'notes', b.notes,
'locationName', b."locationName",
'locationAddress', b."locationAddress",
'listings', CASE
WHEN COUNT(l.id) = 0 THEN 'null'
ELSE json_agg(
json_build_object(
'id', l.id,
'businessId', l."businessId",
'name', l.name,
'description', l.description,
'type', l.type,
'wage', l.wage,
'link', l.link
)
)
END
'locationAddress', b."locationAddress"
)
FROM businesses b
LEFT JOIN listings l ON b.id = l."businessId"
WHERE b.id IN ${'$filters'.replaceAll('[', '(').replaceAll(']', ')')}
WHERE b.id IN (${filters.join(',')})
GROUP BY b.id;
'''));
@@ -413,15 +410,25 @@ void main() async {
print('business logo request received');
var logo = File('logos/$logoId.png');
List<int> content = logo.readAsBytesSync();
return Response.ok(
content,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'image/png'
},
);
try {
List<int> content = logo.readAsBytesSync();
return Response.ok(
content,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'image/png'
},
);
} catch (e) {
print('Error reading logo!');
return Response.notFound(
'logo not found',
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'image/png'
},
);
}
});
app.post('/fbla-api/createbusiness', (Request request) async {
print('create business request received');
@@ -475,8 +482,8 @@ void main() async {
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'}')
INSERT INTO listings ("businessId", name, description, type, wage, link, "offerType")
VALUES ('${listing.businessId}', '${listing.name.replaceAll("'", "''")}', '${listing.description.replaceAll("'", "''")}', '${listing.type.name}', '${listing.wage ?? 'NULL'}', '${listing.link?.replaceAll("'", "''") ?? 'NULL'}', '${listing.offerType.name}')
'''
.replaceAll("'null'", 'NULL'));
@@ -619,7 +626,7 @@ void main() async {
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
"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, "offerType"='${listing.offerType.name}'::text WHERE
id = ${listing.id};
'''
.replaceAll("'null'", 'NULL'));