public async System.Threading.Tasks.Task <ActionResult> PostAsync(ProjectSpeedy.Models.Problem.ProblemUpdate form, string projectId, string problemId) { try { // Checks we have a valid request. if (form == null || !ModelState.IsValid) { return(this.BadRequest()); } // Tries to load the project to check that it exists var problem = await this._problemServices.GetAsync(projectId, problemId); // Ensures the project id is correct. if (problem.ProjectId != ProjectSpeedy.Services.Project.PREFIX + projectId) { return(this.NotFound()); } // Ensures we dont have a problem with the same name already var project = await this._projectService.Get(projectId); if (project.Problems.Any(p => p.Name.Trim().ToLower() == form.Name.Trim().ToLower() && p.Id != ProjectSpeedy.Services.Problem.PREFIX + problemId)) { return(BadRequest(new ProjectSpeedy.Models.General.BadRequest() { Message = "There is already a problem with the same name." })); } // Tries to carry out the save. return(this.Accepted(await this._problemServices.UpdateAsync(projectId, problemId, form))); } catch (HttpRequestException e) { // Cant find the problem if (e.StatusCode == System.Net.HttpStatusCode.NotFound) { return(NotFound()); } // There has been a problem loading or saving data. this._logger.LogError(e, e.Message); return(this.Problem()); } catch (Exception e) { // There has been a system error. this._logger.LogError(e, e.Message); return(this.Problem()); } }
public async System.Threading.Tasks.Task TestUpdateValidAsync() { // Arrange var mockTest = new Mock <ProjectSpeedy.Services.IServiceBase>(); var problemService = new ProjectSpeedy.Services.Problem(mockTest.Object); // Form to go into function var form = new ProjectSpeedy.Models.Problem.ProblemUpdate() { Name = "Test Problem", Description = "Description" }; // Response from document get HttpResponseMessage response = new HttpResponseMessage(); string content = JsonSerializer.Serialize(new ProjectSpeedy.Models.Problem.Problem() { Name = "Problem Name", Description = "Problem Description" }); response.Content = new StringContent(content); mockTest.Setup(d => d.DocumentGet(It.IsAny <string>())) .Returns(Task.FromResult(response.Content)); // Response from view get HttpResponseMessage responseView = new HttpResponseMessage(); string contentView = JsonSerializer.Serialize(new ProjectSpeedy.Models.CouchDb.View.ViewResult() { rows = new List <ProjectSpeedy.Models.CouchDb.View.ListItem>() }); responseView.Content = new StringContent(contentView); mockTest.Setup(d => d.ViewGet(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(Task.FromResult(responseView.Content)); // Response from document update mockTest.Setup(d => d.DocumentUpdate(It.IsAny <string>(), It.IsAny <ProjectSpeedy.Models.Problem.Problem>())) .Returns(Task.FromResult(true)); // Act var test = await problemService.UpdateAsync("ProjectId", "ProblemId", form); // Assert Assert.AreEqual(true, test); }