private IEnumerable <string> ValidateBlock(PostUploadBlock block) { if (string.IsNullOrWhiteSpace(block.index) || !int.TryParse(block.index, out _)) { yield return("Invalid index must be present and be a number."); } if (string.IsNullOrWhiteSpace(block.hash)) { yield return("Invalid hash."); } if (string.IsNullOrWhiteSpace(block.data)) { yield return("Invalid data."); } if (string.IsNullOrWhiteSpace(block.minedBy)) { yield return("Invalid minedBy."); } if (String.IsNullOrWhiteSpace(block.nonce) || !int.TryParse(block.nonce, out _)) { yield return("Invalid nonce must be present and be a number."); } if (string.IsNullOrWhiteSpace(block.previousHash)) { yield return("Invalid previousHash."); } }
public ActionResult Upload([FromBody] PostUploadBlock block) { System.Threading.Thread.Sleep(1000); var validationErrors = ValidateBlock(block); if (validationErrors.Any()) { return(StatusCode((int)HttpStatusCode.BadRequest, new { ok = "false", errorMessage = String.Join(Environment.NewLine, validationErrors) })); } try { var result = TryUploadBlock( int.Parse(block.index), block.minedBy, block.data, block.previousHash, int.Parse(block.nonce), block.hash); if (result.Item1) { return(StatusCode((int)HttpStatusCode.Created, new { ok = true, })); } else { return(StatusCode((int)HttpStatusCode.BadRequest, new { ok = "false", errorMessage = string.Join(Environment.NewLine, result.Item2) })); } } catch (Exception ex) { return(StatusCode((int)HttpStatusCode.BadRequest, new { ok = "false", errorMessage = ex.Message })); } }