private static async Task <IActionResult> ProcessPost(FirebaseClient client, string firebaseId, object data) { (IActionResult result, ProfileObject profile) = ProfileObject.Create(client, firebaseId, data).Result; if (result is BadRequestObjectResult) { return(result); } await client.Child("profile").Child(firebaseId).PutAsync(profile); return(result); }
public async static Task <(IActionResult, ProfileObject)> Create(FirebaseClient client, string firebaseId, dynamic data) { string name = data?.name; if (string.IsNullOrEmpty(name)) { return(new BadRequestObjectResult(new { message = "invalid name" }), null); } string dob = data?.dateOfBirth; DateTime dateOfBirth; if (!DateTime.TryParse(dob, out dateOfBirth)) { return(new BadRequestObjectResult(new { message = "invalid dateOfBirth" }), null); } string w = data?.weight; double weight = 0.0; if (!double.TryParse(w, out weight) || weight < 0) { return(new BadRequestObjectResult(new { message = "invalid weight" }), null); } string h = data?.height; double height = 0.0; if (!double.TryParse(h, out height) || height < 0) { return(new BadRequestObjectResult(new { message = "invalid height" }), null); } string demographic = data?.demographic; if (string.IsNullOrEmpty(demographic)) { return(new BadRequestObjectResult(new { message = "invalid demographic" }), null); } string location = data?.location; if (string.IsNullOrEmpty(location)) { return(new BadRequestObjectResult(new { message = "invalid location" }), null); } var profile = new ProfileObject(name, dateOfBirth, weight, height, demographic, location); var existing = await client.Child("profile").Child(firebaseId).OnceSingleAsync <ProfileObject>(); if (existing != null) { return(new BadRequestObjectResult(new { message = "User already exists." }), null); } return((ActionResult) new OkObjectResult(profile), profile); }