Exemplo n.º 1
0
        public async Task <IActionResult> DeleteTaskAsync(Dictionary <string, string> data, [FromServices] IMongoCollection <DataModels.TaskLists.Task> mongoCollection, [FromServices] IMongoCollection <TaskList> taskListMongoCollection, [FromServices] IMongoCollection <PersonTaskCompletionDeclaration> declarationsMongoCollection)
        {
            Guid   taskID = Guid.Parse(data["taskID"]);
            string owner  = data["owner"];

            DataModels.TaskLists.Task task = await mongoCollection.FirstOrDefaultAsync(x => x.ID == taskID);

            if (task == null)
            {
                return(new JsonResult(new { Type = "Error", Details = "There's no such Task!" }));
            }
            TaskList taskList = await taskListMongoCollection.FirstOrDefaultAsync(x => x.ID == task.ID);

            if (taskList == null || (!taskList.Owner.Contains(owner) && !owner.Contains(taskList.Owner)))
            {
                return(new JsonResult(new { Type = "Error", Details = "You don't have access to that Task!" }));
            }

            DeleteResult result = await mongoCollection.DeleteOneAsync(x => x.ID == taskID);

            DeleteResult result2 = await declarationsMongoCollection.DeleteManyAsync(x => x.Task == taskID);

            if (result.IsAcknowledged && result2.IsAcknowledged)
            {
                return(new JsonResult(new { Type = "Success", Details = "" }));
            }
            else
            {
                return(new JsonResult(new { Type = "Error", Details = "Deletion failed for some reason!" }));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UpdateTaskAsync(DataModels.TaskLists.Task task, [FromServices] IMongoCollection <DataModels.TaskLists.Task> mongoCollection)
        {
            if (task == null || !task.AreValuesCorrect())
            {
                return(new JsonResult(new { Type = "Error", Details = "Task is null or it's properties are empty!" }));
            }

            DataModels.TaskLists.Task currentValue = await mongoCollection.FirstOrDefaultAsync(x => x.ID == task.ID);

            if (currentValue == null)
            {
                return(new JsonResult(new { Type = "Error", Details = "Task's ID has been altered or such Task never existed! Update failed!" }));
            }

            UpdateResult result = await mongoCollection.UpdateOneAsync(x => x.ID == task.ID, Extensions.GenerateUpdateDefinition(currentValue, task));

            if (result.IsAcknowledged)
            {
                return(new JsonResult(new { Type = "Success", Details = "" }));
            }
            else
            {
                return(new JsonResult(new { Type = "Error", Details = "Update somehow failed!" }));
            }
        }
Exemplo n.º 3
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.º 4
0
        public async Task <IActionResult> UpdateTaskAsync(DataModels.TaskLists.Task task,
                                                          [FromServices] IMongoCollection <DataModels.TaskLists.Task> mongoCollection)
        {
            if (task == null || !task.AreValuesCorrect())
            {
                return(this.Error(HttpStatusCode.UnprocessableEntity, "Task is null or it's properties are empty!"));
            }

            DataModels.TaskLists.Task currentValue = await mongoCollection.FirstOrDefaultAsync(x => x.ID == task.ID);

            if (currentValue == null)
            {
                return(this.Error(HttpStatusCode.UnprocessableEntity,
                                  "Task's Id has been altered or such Task never existed! Update failed!"));
            }

            UpdateResult result = await mongoCollection.UpdateOneAsync(x => x.ID == task.ID,
                                                                       Extensions.GenerateUpdateDefinition(currentValue, task));

            if (result.IsAcknowledged)
            {
                return(this.Success(""));
            }
            else
            {
                return(this.Error(HttpStatusCode.InternalServerError, "Update somehow failed!"));
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> DeleteTaskAsync(Dictionary <string, string> data,
                                                          [FromServices] IMongoCollection <DataModels.TaskLists.Task> mongoCollection,
                                                          [FromServices] IMongoCollection <TaskList> taskListMongoCollection,
                                                          [FromServices] IMongoCollection <PersonTaskCompletionDeclaration> declarationsMongoCollection)
        {
            Guid   taskID = Guid.Parse(data["taskId"]);
            string owner  = data["owner"];

            DataModels.TaskLists.Task task = await mongoCollection.FirstOrDefaultAsync(x => x.ID == taskID);

            if (task == null)
            {
                return(this.Error(HttpStatusCode.NotFound, "There's no such Task!"));
            }
            TaskList taskList = await taskListMongoCollection.FirstOrDefaultAsync(x => x.ID == task.ID);

            if (taskList == null || (!taskList.Owner.Contains(owner) && !owner.Contains(taskList.Owner)))
            {
                return(this.Error(HttpStatusCode.Forbidden, "You don't have access to that Task!"));
            }

            DeleteResult result = await mongoCollection.DeleteOneAsync(x => x.ID == taskID);

            DeleteResult result2 = await declarationsMongoCollection.DeleteManyAsync(x => x.Task == taskID);

            if (result.IsAcknowledged && result2.IsAcknowledged)
            {
                return(Ok());
            }
            else
            {
                return(this.Error(HttpStatusCode.InternalServerError, "Deletion failed for some reason!"));
            }
        }
Exemplo n.º 6
0
        public async Task <IActionResult> AddTaskAsync(DataModels.TaskLists.Task task, [FromServices] IMongoCollection <DataModels.TaskLists.Task> mongoCollection)
        {
            if (task == null || !task.AreValuesCorrect())
            {
                return(new JsonResult(new { Type = "Error", Details = "Task is null or it's properties are empty!" }));
            }
            task.ID = Guid.NewGuid();
            await mongoCollection.InsertOneAsync(task);

            return(new JsonResult(new { Type = "Success", Details = task.ID }));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> AddTaskAsync(DataModels.TaskLists.Task task,
                                                       [FromServices] IMongoCollection <DataModels.TaskLists.Task> mongoCollection)
        {
            if (task == null || !task.AreValuesCorrect())
            {
                return(this.Error(HttpStatusCode.UnprocessableEntity, "Task is null or it's properties are empty!"));
            }
            task.ID = Guid.NewGuid();
            await mongoCollection.InsertOneAsync(task);

            return(this.Success(task.ID));
        }
Exemplo n.º 8
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 }));
        }