/// <summary> /// Initializes a new instance of the <see cref="ReportModule"/> class. /// </summary> public ReportModule() : base("/api/user") { StaticConfiguration.DisableErrorTraces = false; Get["/{user_id:guid}/reports/count/{count:int}"] = parameters => { var access_token = Request.Headers.Authorization.Split(' ')[1]; if (DatabaseContext.Instance.TokenValid((string)parameters.user_id, access_token)) { var reports = DatabaseContext.Instance.GetReports(parameters.user_id.ToString()); return Response.AsJson(new Dictionary<string, object> { {"status", "fine"}, {"reports", reports} }, HttpStatusCode.OK); } else { return Response.AsJson(new Dictionary<string, object> { {"status","error"}, {"description","either the user_id doesn't exists or your access_token is invalid."} }, HttpStatusCode.Unauthorized); } }; Post["/{user_id:guid}/reports/add"] = parameters => { dynamic request = JsonConvert.DeserializeObject(this.Request.Body.AsString()); var access_token = Request.Headers.Authorization.Split(' ')[1]; if (DatabaseContext.Instance.TokenValid((string)parameters.user_id, access_token)) { var reportModel = new ReportModel { UserId = (String)parameters.user_id, Title = request.report.Title, Timestamp = request.report.Timestamp, Type = (ReportModel.ReportType)Enum.Parse(typeof(ReportModel.ReportType), (string)request.report.Type, true), FilesNumber = request.report.FilesNumber }; if(DatabaseContext.Instance.AddReport(ref reportModel)) { return Response.AsJson(new Dictionary<string, object> { {"report_id", reportModel.ReportID}, {"status", "fine"}, {"description", "report metadata added successfully, now you can upload files."} }, HttpStatusCode.OK); } } return Response.AsJson(new Dictionary<string, object> { {"status","error"}, {"description","either the user_id doesn't exists or your access_token is invalid."} }, HttpStatusCode.Unauthorized); }; Post["/{user_id:guid}/reports/{report_id}/{file:int}"] = parameters => { var access_token = Request.Headers.Authorization.Split(' ')[1]; if (DatabaseContext.Instance.TokenValid((string)parameters.user_id, (string)access_token)) { if (DatabaseContext.Instance.ReportExists(parameters.report_id)) { if (DatabaseContext.Instance.GetReport(parameters.report_id).FilesNumber>parameters.file && parameters.file>=0) { // Recieve uploaded file and save it somewhere // on the disk with a properly formatted path. string filePath = Path.Combine(Utils.FILE_BASE_PATH + (string)parameters.user_id + @"\reports\"); var temp = !Directory.Exists(filePath)? Directory.CreateDirectory(filePath): null; filePath += parameters.report_id + "_" + (string)parameters.file + "." + Request.Headers.ContentType.Split('/')[1]; using (var fileStream = new FileStream(filePath, FileMode.Create)) { Request.Body.CopyTo(fileStream); return Response.AsJson(new Dictionary<string, object> { {"status", "ok"}, {"description", "image uploaded succesfully"} }); } } } } return Response.AsJson(new Dictionary<string, object> { {"status","error"}, {"description","either the user_id doesn't exists or your access_token is invalid."} }, HttpStatusCode.Unauthorized); }; Get["/{user_id:guid}/reports/{report_id}/{file:int}"] = parameters => { var access_token = Request.Headers.Authorization.Split(' ')[1]; if (DatabaseContext.Instance.TokenValid((string)parameters.user_id, (string)access_token)) { if (DatabaseContext.Instance.ReportExists(parameters.report_id)) { if (DatabaseContext.Instance.GetReport(parameters.report_id).FilesNumber > parameters.file && parameters.file > 0) { // Recieve uploaded file and save it somewhere // on the disk with a properly formatted path. string filePath = Path.Combine(Utils.FILE_BASE_PATH + (string)parameters.user_id + @"\reports\"); filePath += parameters.report_id + "_" + (string)parameters.file + "." + Request.Headers.ContentType.Split('/')[1]; return Response.AsFile(filePath); } else { return Response.AsJson(new Dictionary<string, object> { {"status", "error"}, {"description", "file number out of range"} }, HttpStatusCode.RequestedRangeNotSatisfiable); } } else { return Response.AsJson(new Dictionary<string, object> { {"status", "error"}, {"description", "report_id doesn't exists"} }, HttpStatusCode.NotFound); } } else { return Response.AsJson(new Dictionary<string, object> { {"status","error"}, {"description","either the user_id doesn't exists or your access_token is invalid."} }, HttpStatusCode.Unauthorized); } }; }
/// <summary> /// Adds the report. /// </summary> /// <param name="report">The report.</param> /// <returns></returns> public bool AddReport(ref ReportModel report) { if (UserExists(report.UserId)) { // Create new report ID using a combined hash of user-id and // timiestamp. report.ReportID = String.Format("{0:X}", (report.UserId+report.Timestamp.ToString()).GetHashCode()); var collection = database.GetCollection<ReportModel>("reports"); collection.InsertOneAsync(report).ConfigureAwait(false); collection.InsertOneAsync(report); return true; } else { // User doesn't exixts, hope to provide some meaningful error // messages in the future. return false; } }
/// <summary> /// Modifies the report. /// </summary> /// <param name="report">The report.</param> /// <returns></returns> public bool ModifyReport(ReportModel report) { if (ReportExists(report.ReportID)) { var collection = database.GetCollection<ReportModel>("reports"); collection.DeleteOneAsync(Builders<ReportModel>.Filter.Eq("ReportID", report.ReportID)).ConfigureAwait(false); collection.DeleteOneAsync(Builders<ReportModel>.Filter.Eq("ReportID", report.ReportID)); collection.InsertOneAsync(report).ConfigureAwait(false); collection.InsertOneAsync(report); return true; } else { // Again, a better error in future. return false; } }
/// <summary> /// Initializes a new instance of the <see cref="ReportModule"/> class. /// </summary> public ReportModule() : base("/api/user") { StaticConfiguration.DisableErrorTraces = false; Get["/{user_id:guid}/reports/count/{count:int}"] = parameters => { var access_token = Request.Headers.Authorization.Split(' ')[1]; if (DatabaseContext.Instance.TokenValid((string)parameters.user_id, access_token)) { var reports = DatabaseContext.Instance.GetReports(parameters.user_id.ToString()); return(Response.AsJson(new Dictionary <string, object> { { "status", "fine" }, { "reports", reports } }, HttpStatusCode.OK)); } else { return(Response.AsJson(new Dictionary <string, object> { { "status", "error" }, { "description", "either the user_id doesn't exists or your access_token is invalid." } }, HttpStatusCode.Unauthorized)); } }; Post["/{user_id:guid}/reports/add"] = parameters => { dynamic request = JsonConvert.DeserializeObject(this.Request.Body.AsString()); var access_token = Request.Headers.Authorization.Split(' ')[1]; if (DatabaseContext.Instance.TokenValid((string)parameters.user_id, access_token)) { var reportModel = new ReportModel { UserId = (String)parameters.user_id, Title = request.report.Title, Timestamp = request.report.Timestamp, Type = (ReportModel.ReportType)Enum.Parse(typeof(ReportModel.ReportType), (string)request.report.Type, true), FilesNumber = request.report.FilesNumber }; if (DatabaseContext.Instance.AddReport(ref reportModel)) { return(Response.AsJson(new Dictionary <string, object> { { "report_id", reportModel.ReportID }, { "status", "fine" }, { "description", "report metadata added successfully, now you can upload files." } }, HttpStatusCode.OK)); } } return(Response.AsJson(new Dictionary <string, object> { { "status", "error" }, { "description", "either the user_id doesn't exists or your access_token is invalid." } }, HttpStatusCode.Unauthorized)); }; Post["/{user_id:guid}/reports/{report_id}/{file:int}"] = parameters => { var access_token = Request.Headers.Authorization.Split(' ')[1]; if (DatabaseContext.Instance.TokenValid((string)parameters.user_id, (string)access_token)) { if (DatabaseContext.Instance.ReportExists(parameters.report_id)) { if (DatabaseContext.Instance.GetReport(parameters.report_id).FilesNumber > parameters.file && parameters.file >= 0) { // Recieve uploaded file and save it somewhere // on the disk with a properly formatted path. string filePath = Path.Combine(Utils.FILE_BASE_PATH + (string)parameters.user_id + @"\reports\"); var temp = !Directory.Exists(filePath)? Directory.CreateDirectory(filePath): null; filePath += parameters.report_id + "_" + (string)parameters.file + "." + Request.Headers.ContentType.Split('/')[1]; using (var fileStream = new FileStream(filePath, FileMode.Create)) { Request.Body.CopyTo(fileStream); return(Response.AsJson(new Dictionary <string, object> { { "status", "ok" }, { "description", "image uploaded succesfully" } })); } } } } return(Response.AsJson(new Dictionary <string, object> { { "status", "error" }, { "description", "either the user_id doesn't exists or your access_token is invalid." } }, HttpStatusCode.Unauthorized)); }; Get["/{user_id:guid}/reports/{report_id}/{file:int}"] = parameters => { var access_token = Request.Headers.Authorization.Split(' ')[1]; if (DatabaseContext.Instance.TokenValid((string)parameters.user_id, (string)access_token)) { if (DatabaseContext.Instance.ReportExists(parameters.report_id)) { if (DatabaseContext.Instance.GetReport(parameters.report_id).FilesNumber > parameters.file && parameters.file > 0) { // Recieve uploaded file and save it somewhere // on the disk with a properly formatted path. string filePath = Path.Combine(Utils.FILE_BASE_PATH + (string)parameters.user_id + @"\reports\"); filePath += parameters.report_id + "_" + (string)parameters.file + "." + Request.Headers.ContentType.Split('/')[1]; return(Response.AsFile(filePath)); } else { return(Response.AsJson(new Dictionary <string, object> { { "status", "error" }, { "description", "file number out of range" } }, HttpStatusCode.RequestedRangeNotSatisfiable)); } } else { return(Response.AsJson(new Dictionary <string, object> { { "status", "error" }, { "description", "report_id doesn't exists" } }, HttpStatusCode.NotFound)); } } else { return(Response.AsJson(new Dictionary <string, object> { { "status", "error" }, { "description", "either the user_id doesn't exists or your access_token is invalid." } }, HttpStatusCode.Unauthorized)); } }; }