Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
52 changed files
with
1,429 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Files and directories created by pub. | ||
.dart_tool/ | ||
.packages | ||
|
||
# Conventional directory for build output. | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 1.0.0 | ||
|
||
- Initial version. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
A simple command-line application. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This file configures the static analysis results for your project (errors, | ||
# warnings, and lints). | ||
# | ||
# This enables the 'recommended' set of lints from `package:lints`. | ||
# This set helps identify many issues that may lead to problems when running | ||
# or consuming Dart code, and enforces writing Dart using a single, idiomatic | ||
# style and format. | ||
# | ||
# If you want a smaller set of lints you can change this to specify | ||
# 'package:lints/core.yaml'. These are just the most critical lints | ||
# (the recommended set includes the core lints). | ||
# The core lints are also what is used by pub.dev for scoring packages. | ||
|
||
include: package:lints/recommended.yaml | ||
|
||
# Uncomment the following section to specify additional rules. | ||
|
||
# linter: | ||
# rules: | ||
# - camel_case_types | ||
|
||
# analyzer: | ||
# exclude: | ||
# - path/to/excluded/files/** | ||
|
||
# For more information about the core and recommended set of lints, see | ||
# https://dart.dev/go/core-lints | ||
|
||
# For additional information about configuring this file, see | ||
# https://dart.dev/guides/language/analysis-options |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:args/args.dart'; | ||
import 'package:shelf/shelf.dart' as shelf; | ||
import 'package:shelf/shelf_io.dart' as io; | ||
import 'package:shelf_hotreload/shelf_hotreload.dart'; | ||
import 'db/mongo_config.dart'; | ||
import 'routes/init.dart'; | ||
import 'utils/cors.dart'; | ||
|
||
void main(List<String> arguments) async { | ||
await DBSetup.init(); | ||
withHotreload(() => createServer(arguments)); | ||
} | ||
|
||
Future<HttpServer> createServer(List<String> args) { | ||
handler(shelf.Request request) { | ||
print(request.url); | ||
return InitRoute().handler(request); | ||
} | ||
|
||
var parser = ArgParser() | ||
..addOption( | ||
'port', | ||
abbr: 'p', | ||
); | ||
var result = parser.parse( | ||
args, | ||
); | ||
var portStr = result['port'] ?? Platform.environment['PORT'] ?? '8080'; | ||
var port = int.tryParse( | ||
portStr, | ||
); | ||
|
||
if (port == null) { | ||
stdout.writeln( | ||
'Could not parse port value "$portStr" into a number.', | ||
); | ||
exitCode = 64; | ||
} | ||
var updateHandler = const shelf.Pipeline() | ||
.addMiddleware(corsHeaders()) | ||
.addMiddleware(shelf.logRequests()) | ||
.addHandler(handler); | ||
return io.serve(updateHandler, '192.168.1.162', 8080); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:mongo_dart/mongo_dart.dart'; | ||
import 'package:shelf/shelf.dart'; | ||
import '../utils/extensions.dart'; | ||
import '../db/mongo_config.dart'; | ||
import 'image.controller.dart'; | ||
|
||
class ArtController { | ||
Future<Response> getAllArts( | ||
Request request, | ||
) async { | ||
try { | ||
final pipeline = AggregationPipelineBuilder() | ||
.addStage(Lookup( | ||
from: 'users', | ||
localField: 'userId', | ||
foreignField: '_id', | ||
as: "seller")) | ||
.addStage(Match({ | ||
'status': {"\$ne": "SOLD"} | ||
})) | ||
.build(); | ||
var result = await DBSetup().artsRef.aggregateToStream(pipeline).toList(); | ||
|
||
return Response.ok( | ||
jsonEncode(result), | ||
); | ||
} catch (e) { | ||
return Response.internalServerError( | ||
body: jsonEncode({"message": "$e"}), | ||
); | ||
} | ||
} | ||
|
||
Future<Response> getArtById(Request request, String artId) async { | ||
try { | ||
var result = await DBSetup() | ||
.artsRef | ||
.findOne({"_id": ObjectId.fromHexString(artId)}); | ||
if (result == null) { | ||
return Response.notFound({"message": "No art found with this Id"}); | ||
} | ||
return Response.ok( | ||
jsonEncode(result), | ||
); | ||
} catch (e) { | ||
return Response.internalServerError( | ||
body: jsonEncode({"message": "$e"}), | ||
); | ||
} | ||
} | ||
|
||
Future<Response> createArt( | ||
Request request, | ||
) async { | ||
if (!request.isMultipart) { | ||
return Response.badRequest( | ||
body: jsonEncode({"message": "Please send in form data"})); | ||
} | ||
try { | ||
List<FormData> data; | ||
try { | ||
data = await request.multipartFormData.toList(); | ||
} catch (e) { | ||
return Response.internalServerError( | ||
body: jsonEncode({'message': "Error in multipart conversion:$e"})); | ||
} | ||
Map<String, dynamic> body = {}; | ||
|
||
for (var element in data) { | ||
var key = element.name; | ||
if (element.name == 'file') { | ||
var imageBytes = (await element.part.readBytes()); | ||
var res = await ImageController().uploadImage(imageBytes, | ||
DateTime.now().millisecondsSinceEpoch.toString() + ".jpg"); | ||
print('Image Upload Response - $res'); | ||
if (res is String) { | ||
body['image'] = res; | ||
} else { | ||
return Response.internalServerError(); | ||
} | ||
} else { | ||
var value = await element.part.readString(); | ||
body[key] = value; | ||
} | ||
} | ||
print("Body for create art - $body"); | ||
if (body.values.contains(null) || body.isEmpty) { | ||
return Response.badRequest( | ||
body: jsonEncode({"message": "Missing required fields"}), | ||
); | ||
} | ||
var newArt = { | ||
"userId": ObjectId.fromHexString(body['userId']), | ||
"title": body['title'], | ||
'status': body['status'] ?? "AVAILABLE", | ||
"amount": double.tryParse(body['amount']) ?? 0.0, | ||
"description": body['description'], | ||
'createdAt': DateTime.now().toIso8601String(), | ||
'updatedAt': DateTime.now().toIso8601String(), | ||
'image': body['image'] | ||
}; | ||
var dbRes = await DBSetup().artsRef.insertOne( | ||
newArt, | ||
); | ||
if (dbRes.hasWriteErrors) { | ||
return Response.internalServerError( | ||
body: jsonEncode({"message": "Art already exists"}), | ||
); | ||
} | ||
|
||
return Response.ok(jsonEncode(dbRes.document)); | ||
} catch (e) { | ||
return Response.internalServerError( | ||
body: jsonEncode({"message": "$e"}), | ||
); | ||
} | ||
} | ||
|
||
Future<Response> updateStatus(Request request, String artId) async { | ||
try { | ||
var body = | ||
Map<String, dynamic>.from(jsonDecode(await request.readAsString())); | ||
var isArtExists = await DBSetup() | ||
.artsRef | ||
.findOne({"_id": ObjectId.fromHexString(artId)}); | ||
print("Is Art Exists - ${isArtExists != null} and id - $artId"); | ||
if (isArtExists != null) { | ||
isArtExists['status'] = body['status']; | ||
await DBSetup().artsRef.replaceOne( | ||
{"_id": ObjectId.fromHexString(artId)}, | ||
isArtExists, | ||
); | ||
return Response.ok(jsonEncode({"message": "Updated art successfully"})); | ||
} else { | ||
return Response.notFound(jsonEncode({"message": "Art not exist"})); | ||
} | ||
} catch (e) { | ||
return Response.internalServerError( | ||
body: jsonEncode({"message": "$e"}), | ||
); | ||
} | ||
} | ||
|
||
Future<Response> getArtsPerUser(Request request, String userId) async { | ||
try { | ||
final pipeline = AggregationPipelineBuilder() | ||
.addStage(Lookup( | ||
from: 'users', | ||
localField: 'userId', | ||
foreignField: '_id', | ||
as: "seller")) | ||
.addStage(Match({'userId': ObjectId.fromHexString(userId)})) | ||
.build(); | ||
var result = await DBSetup().artsRef.aggregateToStream(pipeline).toList(); | ||
|
||
return Response.ok( | ||
jsonEncode(result), | ||
); | ||
} catch (e) { | ||
return Response.internalServerError( | ||
body: jsonEncode({"message": "$e"}), | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.