public IActionResult UpdateTimeSpan([FromRoute] int id, [FromBody] UpdateTimeSpanRequest request) { if (!IsAuthorized()) { return(Unauthorized()); } try { var facade = new HelpdeskFacade(); var response = facade.UpdateTimeSpan(id, request); switch (response.Status) { case HttpStatusCode.OK: return(Ok(response)); case HttpStatusCode.BadRequest: return(BadRequest(BuildBadRequestMessage(response))); case HttpStatusCode.InternalServerError: return(StatusCode(StatusCodes.Status500InternalServerError)); case HttpStatusCode.NotFound: return(NotFound()); } s_logger.Fatal("This code should be unreachable, unknown result has occured."); } catch (Exception ex) { s_logger.Error(ex, "Unable to update timespan."); } return(StatusCode(StatusCodes.Status500InternalServerError)); }
/// <summary> /// This method updates a specified timespan's information in the database /// </summary> /// <param name="id">The SpanId of the timespan to be updated</param> /// <param name="request">The request that contains the timespan's new information</param> /// <returns>A bool indicating whether the operation was a success</returns> public bool UpdateTimeSpan(int id, UpdateTimeSpanRequest request) { using (helpdesksystemContext context = new helpdesksystemContext()) { Timespans timespan = context.Timespans.FirstOrDefault(t => t.SpanId == id); if (timespan == null) { return(false); } Timespans existingTimespan = null; existingTimespan = context.Timespans.FirstOrDefault(t => t.Name == request.Name); // Update if no timespan exists matching the requesting name. // Update anyway if the names match but the the existing timespan is the timespan we want to update. if (existingTimespan == null || existingTimespan.SpanId == id) { timespan.Name = request.Name; timespan.StartDate = request.StartDate; timespan.EndDate = request.EndDate; context.SaveChanges(); } else { throw new DuplicateNameException("The nickname " + request.Name + " already exists!"); } } return(true); }
public void UpdateTimespanNoName() { HelpdeskFacade helpdeskFacade = new HelpdeskFacade(); AddTimeSpanRequest addTimeSpanRequest = new AddTimeSpanRequest() { HelpdeskId = 1, StartDate = DateTime.Today, EndDate = DateTime.Today.AddYears(1), Name = AlphaNumericStringGenerator.GetString(10) }; AddTimeSpanResponse addTimeSpanResponse = helpdeskFacade.AddTimeSpan(addTimeSpanRequest); Assert.AreEqual(HttpStatusCode.OK, addTimeSpanResponse.Status); UpdateTimeSpanRequest updateTimespanRequest = new UpdateTimeSpanRequest() { StartDate = new DateTime(2019, 01, 01), EndDate = new DateTime(2019, 06, 01), }; UpdateTimeSpanResponse updateTimespanResponse = helpdeskFacade.UpdateTimeSpan(addTimeSpanResponse.SpanId, updateTimespanRequest); Assert.AreEqual(HttpStatusCode.BadRequest, updateTimespanResponse.Status); }
/// <summary> /// This method is responsible for updating a specific timespan's information /// </summary> /// <param name="id">The SpanId of the timespan to be updated</param> /// <param name="request">The timespan's new information</param> /// <returns>The response that indicates if the operation was a success</returns> public UpdateTimeSpanResponse UpdateTimeSpan(int id, UpdateTimeSpanRequest request) { s_logger.Info("Updating timespan..."); UpdateTimeSpanResponse response = new UpdateTimeSpanResponse(); try { response = (UpdateTimeSpanResponse)request.CheckValidation(response); if (response.Status == HttpStatusCode.BadRequest) { return(response); } var dataLayer = new HelpdeskDataLayer(); //will implement unique check when get timespan by name method is implemented bool result = dataLayer.UpdateTimeSpan(id, request); if (result == false) { throw new NotFoundException("Unable to find timespan!"); } response.Result = result; response.Status = HttpStatusCode.OK; } catch (NotFoundException ex) { s_logger.Error(ex, "Unable to find timespan!"); response.Status = HttpStatusCode.NotFound; response.StatusMessages.Add(new StatusMessage(HttpStatusCode.NotFound, "Unable to find timespan!")); } catch (DuplicateNameException ex) { s_logger.Error(ex, "Timespan name already exists!"); response.Status = HttpStatusCode.BadRequest; response.StatusMessages.Add(new StatusMessage(HttpStatusCode.BadRequest, "Timespan name already exists!")); } catch (Exception ex) { s_logger.Error(ex, "Unable to update timespan!"); response.Status = HttpStatusCode.InternalServerError; response.StatusMessages.Add(new StatusMessage(HttpStatusCode.InternalServerError, "Unable to update timespan!")); } return(response); }
public void UpdateTimespanNotFound() { HelpdeskFacade helpdeskFacade = new HelpdeskFacade(); UpdateTimeSpanRequest updateTimespanRequest = new UpdateTimeSpanRequest() { Name = AlphaNumericStringGenerator.GetString(10), StartDate = new DateTime(2019, 08, 01), EndDate = new DateTime(2019, 11, 01) }; UpdateTimeSpanResponse updateTimespanResponse = helpdeskFacade.UpdateTimeSpan(-1, updateTimespanRequest); Assert.AreEqual(HttpStatusCode.NotFound, updateTimespanResponse.Status); }
public void UpdateTimespan() { // Fill empty string parameters "" with auto-generated string. testEntityFactory.PopulateEmptyStrings = true; // Add test helpdesk. TestDataHelpdesk helpdeskData = testEntityFactory.AddHelpdesk(); // Check that helpdesk was created successfully. Assert.AreEqual(HttpStatusCode.OK, helpdeskData.Response.Status); Assert.IsTrue(helpdeskData.Response.HelpdeskID > 0); // Add timespan. TestDataTimeSpan timespanData = testEntityFactory.AddTimeSpan(helpdeskData.Response.HelpdeskID); // Check that timespan was created successfully. Assert.AreEqual(HttpStatusCode.OK, timespanData.Response.Status); Assert.IsTrue(timespanData.Response.SpanId > 0); UpdateTimeSpanRequest updateTimespanRequest = new UpdateTimeSpanRequest() { Name = AlphaNumericStringGenerator.GetString(10), StartDate = new DateTime(2019, 01, 01), EndDate = new DateTime(2019, 06, 01), }; UpdateTimeSpanResponse updateTimespanResponse = testEntityFactory.HelpdeskFacade.UpdateTimeSpan(timespanData.Response.SpanId, updateTimespanRequest); Assert.AreEqual(HttpStatusCode.OK, updateTimespanResponse.Status); Assert.IsTrue(updateTimespanResponse.Result); using (helpdesksystemContext context = new helpdesksystemContext()) { var timespan = context.Timespans.FirstOrDefault(u => u.SpanId == timespanData.Response.SpanId); Assert.AreEqual(timespan.StartDate, updateTimespanRequest.StartDate); Assert.AreEqual(timespan.Name, updateTimespanRequest.Name); Assert.AreEqual(timespan.EndDate, updateTimespanRequest.EndDate); } }
public void UpdateTimespanNameExists() { // Fill empty string parameters "" with auto-generated string. testEntityFactory.PopulateEmptyStrings = true; // Add test helpdesk. TestDataHelpdesk helpdeskData = testEntityFactory.AddHelpdesk(); // Check that helpdesk was created successfully. Assert.AreEqual(HttpStatusCode.OK, helpdeskData.Response.Status); Assert.IsTrue(helpdeskData.Response.HelpdeskID > 0); // Add timespan. TestDataTimeSpan timespanDataA = testEntityFactory.AddTimeSpan(helpdeskData.Response.HelpdeskID); // Check that timespan was created successfully. Assert.AreEqual(HttpStatusCode.OK, timespanDataA.Response.Status); Assert.IsTrue(timespanDataA.Response.SpanId > 0); // Add another timespan TestDataTimeSpan timespanDataB = testEntityFactory.AddTimeSpan(helpdeskData.Response.HelpdeskID); // Check that timespan was created successfully. Assert.AreEqual(HttpStatusCode.OK, timespanDataB.Response.Status); Assert.IsTrue(timespanDataB.Response.SpanId > 0); // This request will try to update timespan B's name to be the same as A's name, which should fail. var updateTimespanRequest = new UpdateTimeSpanRequest() { Name = timespanDataA.Request.Name, StartDate = new DateTime(2019, 01, 01), EndDate = new DateTime(2019, 06, 01), }; var updateTimespanResponse = testEntityFactory.HelpdeskFacade.UpdateTimeSpan(timespanDataB.Response.SpanId, updateTimespanRequest); Assert.AreEqual(HttpStatusCode.BadRequest, updateTimespanResponse.Status); Assert.IsFalse(updateTimespanResponse.Result); }