Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        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 }));
        }