public async Task <IActionResult> UpdateDeclarationAsync(PersonTaskCompletionDeclaration declaration, [FromServices] IMongoCollection <PersonTaskCompletionDeclaration> mongoCollection) { Guid declarationId = declaration.ID; if (declarationId == Guid.Empty) { return(this.Error(HttpStatusCode.UnprocessableEntity, "Sent Guid is empty!")); } var result = await mongoCollection.FirstOrDefaultAsync(x => x.ID == declarationId); if (result == null) { return(this.Error(HttpStatusCode.NotFound, "There's no such Declaration!")); } var newValue = result; newValue.IsCompleted = true; var output = await mongoCollection.UpdateOneAsync(x => x.ID == declarationId, Extensions.GenerateUpdateDefinition(result, newValue)); return(output.IsAcknowledged ? Ok() : this.Error(HttpStatusCode.InternalServerError, "Update process somehow failed!")); }
public async Task <IActionResult> AddTaskCompletionDeclarationAsync(PersonTaskCompletionDeclaration declaration, [FromServices] IMongoCollection <PersonTaskCompletionDeclaration> mongoCollection, [FromServices] IMongoCollection <DataModels.TaskLists.Task> tasksMongoCollection) { if (declaration == null || !declaration.AreValuesCorrect()) { return(this.Error(HttpStatusCode.UnprocessableEntity, "PersonTaskCompletionDeclaration is null or it's properties are empty!")); } DataModels.TaskLists.Task task = await tasksMongoCollection.FirstOrDefaultAsync(x => x.ID == declaration.Task); if (task == null) { return(this.Error(HttpStatusCode.BadRequest, "PersonTaskCompletionDeclaration has been created for wrong Task!")); } long howManyDeclarationForGiveTask = await mongoCollection.CountDocumentsAsync(x => x.Task == task.ID); if (howManyDeclarationForGiveTask >= task.MaximumCountOfPeopleWhoCanDoIt) { return(this.Error(HttpStatusCode.BadRequest, "You cannot complete that task, because there's maximum amount of people declared.")); } declaration.ID = Guid.NewGuid(); await mongoCollection.InsertOneAsync(declaration); return(this.Success(declaration.ID)); }
public async Task <IActionResult> AddTaskCompletionDeclarationAsync(PersonTaskCompletionDeclaration declaration, [FromServices] IMongoCollection <PersonTaskCompletionDeclaration> mongoCollection, [FromServices] IMongoCollection <DataModels.TaskLists.Task> tasksMongoCollection) { if (declaration == null || !declaration.AreValuesCorrect()) { return(new JsonResult(new { Type = "Error", Details = "PersonTaskCompletionDeclaration is null or it's properties are empty!" })); } DataModels.TaskLists.Task task = await tasksMongoCollection.FirstOrDefaultAsync(x => x.ID == declaration.Task); if (task == null) { return(new JsonResult(new { Type = "Error", Details = "PersonTaskCompletionDeclaration has been created for wrong Task!" })); } declaration.ID = Guid.NewGuid(); await mongoCollection.InsertOneAsync(declaration); return(new JsonResult(new { Type = "Success", Details = declaration.ID })); }