public async Task <IActionResult> PutTaskGroupAsync([FromBody] TasksGroupResource newTaskGroupResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            if (newTaskGroupResource == null)
            {
                return(BadRequest("New tasks group resource is null"));
            }

            mLogger.LogDebug($"Requesting putting new group {newTaskGroupResource.GroupName}");

            try
            {
                IResponse <ITasksGroup> result =
                    await mTasksGroupService.SaveAsync(newTaskGroupResource.GroupName).ConfigureAwait(false);

                if (!result.IsSuccess)
                {
                    return(StatusCode(StatusCodes.Status405MethodNotAllowed, result.Message));
                }

                TasksGroupResource tasksGroupResource = mMapper.Map <ITasksGroup, TasksGroupResource>(result.ResponseObject);
                return(Ok(tasksGroupResource));
            }
            catch (Exception ex)
            {
                mLogger.LogError(ex, "Put operation failed with error");
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }
        public async Task <IActionResult> RemoveGroup(string id)
        {
            mLogger.LogDebug($"Requesting deleting group id {id}");

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            try
            {
                IResponse <ITasksGroup> result = await mTasksGroupService.RemoveAsync(id).ConfigureAwait(false);

                mLogger.LogDebug($"Remove result {(result.IsSuccess ? "succeeded" : "failed")}");

                if (!result.IsSuccess)
                {
                    return(StatusCode(StatusCodes.Status405MethodNotAllowed, result.Message));
                }

                TasksGroupResource tasksGroupResource = mMapper.Map <ITasksGroup, TasksGroupResource>(result.ResponseObject);
                return(Ok(tasksGroupResource));
            }
            catch (Exception ex)
            {
                mLogger.LogError(ex, "Remove operation failed with error");
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }
        public async Task <IActionResult> PostGroupAsync(string id, [FromBody] TasksGroupResource saveTasksGroupResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            if (saveTasksGroupResource == null)
            {
                return(BadRequest("New group name resource is null"));
            }

            mLogger.LogDebug($"Requesting updating group id {id} with name {saveTasksGroupResource.GroupName}");

            try
            {
                IResponse <ITasksGroup> result =
                    await mTasksGroupService.UpdateGroupAsync(id, saveTasksGroupResource.GroupName).ConfigureAwait(false);

                mLogger.LogDebug($"Update result {(result.IsSuccess ? "succeeded" : "failed")}");

                if (!result.IsSuccess)
                {
                    return(StatusCode(StatusCodes.Status405MethodNotAllowed, result.Message));
                }

                TasksGroupResource tasksGroupResource = mMapper.Map <ITasksGroup, TasksGroupResource>(result.ResponseObject);
                return(Ok(tasksGroupResource));
            }
            catch (Exception ex)
            {
                mLogger.LogError(ex, "Update operation failed with error");
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }
Ejemplo n.º 4
0
        private async Task <int> CreateNewTaskGroup(string taskGroupName)
        {
            if (string.IsNullOrWhiteSpace(taskGroupName))
            {
                mLogger.LogError($"{nameof(taskGroupName)} is null or empty");
                return(1);
            }

            TasksGroupResource tasksGroupResource = new TasksGroupResource
            {
                GroupName = taskGroupName
            };

            using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, TaskerUris.TasksGroupUri)
                  {
                      Content = new StringContent(JsonConvert.SerializeObject(tasksGroupResource), Encoding.UTF8, PostMediaType)
                  };

            TasksGroupResource tasksGroupResponse = await HttpMessageRequester.SendHttpRequestMessage <TasksGroupResource>(
                mHttpClient, httpRequestMessage, mLogger).ConfigureAwait(false);

            if (tasksGroupResponse != null)
            {
                mLogger.LogInformation($"Created new group id {tasksGroupResponse.GroupId} '{tasksGroupResponse.GroupName}'");
            }

            return(0);
        }
Ejemplo n.º 5
0
        public async Task PostGroupAsync_AlreadyExistingGroupName_MethodNotAllowedStatusCode()
        {
            const string realGroupId = "1001";
            const string alreadyExistingGroupName = "Free";

            using TestServer testServer = ApiTestHelper.CreateTestServer();
            using HttpClient httpClient = testServer.CreateClient();

            TasksGroupResource groupResource = new TasksGroupResource {
                GroupName = alreadyExistingGroupName
            };

            using StringContent jsonContent    = new StringContent(JsonConvert.SerializeObject(groupResource), Encoding.UTF8, PostMediaType);
            using HttpResponseMessage response = await httpClient.PostAsync($"{MainRoute}/{realGroupId}", jsonContent).ConfigureAwait(false);

            Assert.Equal(HttpStatusCode.MethodNotAllowed, response.StatusCode);
        }
Ejemplo n.º 6
0
        public async Task PostGroupAsync_ThrowsException_InternalServerErrorStatusCode()
        {
            ITasksGroupService fakeTasksGroupService = A.Fake <ITasksGroupService>();

            A.CallTo(() => fakeTasksGroupService.UpdateGroupAsync(A <string> .Ignored, A <string> .Ignored))
            .Throws <Exception>();

            using TestServer testServer = ApiTestHelper.BuildTestServerWithFakes(fakeTasksGroupService);
            using HttpClient httpClient = testServer.CreateClient();

            TasksGroupResource groupResource = new TasksGroupResource {
                GroupName = "newGroupName"
            };

            using StringContent jsonContent    = new StringContent(JsonConvert.SerializeObject(groupResource), Encoding.UTF8, PostMediaType);
            using HttpResponseMessage response = await httpClient.PostAsync($"{MainRoute}/some-id", jsonContent).ConfigureAwait(false);

            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
        }
Ejemplo n.º 7
0
        public async Task PostGroupAsync_SuccessStatusCode()
        {
            ITasksGroupService fakeTasksGroupService = A.Fake <ITasksGroupService>();

            A.CallTo(() => fakeTasksGroupService.UpdateGroupAsync(A <string> .Ignored, A <string> .Ignored))
            .Returns(new SuccessResponse <ITasksGroup>(A.Fake <ITasksGroup>()));

            using TestServer testServer = ApiTestHelper.BuildTestServerWithFakes(fakeTasksGroupService);
            using HttpClient httpClient = testServer.CreateClient();

            TasksGroupResource groupResource = new TasksGroupResource {
                GroupName = "newGroupName"
            };

            using StringContent jsonContent    = new StringContent(JsonConvert.SerializeObject(groupResource), Encoding.UTF8, PostMediaType);
            using HttpResponseMessage response = await httpClient.PostAsync($"{MainRoute}/some-id", jsonContent).ConfigureAwait(false);

            response.EnsureSuccessStatusCode();
        }
Ejemplo n.º 8
0
        private async Task <int> RemoveTaskGroup(string taskGroup)
        {
            if (string.IsNullOrEmpty(taskGroup))
            {
                mLogger.LogError("No group name or group id given");
                return(1);
            }

            Uri groupUri = TaskerUris.TasksGroupUri.CombineRelative(taskGroup);

            using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Delete, groupUri);

            TasksGroupResource tasksGroupResource = await HttpMessageRequester.SendHttpRequestMessage <TasksGroupResource>(
                mHttpClient, httpRequestMessage, mLogger).ConfigureAwait(false);

            if (tasksGroupResource != null)
            {
                mLogger.LogInformation($"Deleted group id {tasksGroupResource.GroupId} {tasksGroupResource.GroupName}");
            }

            return(0);
        }