public async Task UpdateSession_HappyPath_ShouldReturnOk() { //Arrange var endDate = DateTime.Now; var startDate = DateTime.Now.AddDays(-1); var updatedSession = new UpdateSessionDto(); var mockSessionService = A.Fake <ISessionService>(); A.CallTo(() => mockSessionService.UpdateSession(updatedSession)).Returns( new ServiceResponse <GetSessionDto> { Data = new GetSessionDto { Id = 1, StartDateTime = startDate, EndDateTime = endDate } }); var controller = new SessionController(mockSessionService); //Act var result = await controller.UpdateSession(updatedSession); var okResult = result as OkObjectResult; var actualResult = okResult.Value as ServiceResponse <GetSessionDto>; //Assert Assert.AreEqual(typeof(OkObjectResult), result.GetType()); Assert.AreEqual(1, actualResult.Data.Id); Assert.AreEqual(startDate, actualResult.Data.StartDateTime); Assert.AreEqual(endDate, actualResult.Data.EndDateTime); }
public void Update(UpdateSessionDto dto) { var session = _DB.Sessions.Find(dto.Id); _mapper.Map <UpdateSessionDto, SessionDbEntity>(dto, session); _DB.Sessions.Update(session); _DB.SaveChanges(); }
public IActionResult Update(UpdateSessionDto dto) { if (ModelState.IsValid) { _service.Update(dto); return(RedirectToAction("Index")); } return(View(dto)); }
public async Task <IActionResult> UpdateSession(UpdateSessionDto updatedSession) { ServiceResponse <GetSessionDto> response = await _sessionService.UpdateSession(updatedSession); if (response.Message == "Session not found.") { return(NotFound(response)); } if (response.Message == "EndDateTime cannot be earlier than StartDateTime.") { return(BadRequest(response)); } return(Ok(response)); }
public async Task UpdateSession_HappyPath_ShouldReturnNotFound() { //Arrange var updatedSession = new UpdateSessionDto(); var mockSessionService = A.Fake <ISessionService>(); A.CallTo(() => mockSessionService.UpdateSession(updatedSession)).Returns( new ServiceResponse <GetSessionDto> { Data = null, Message = "Session not found." }); var controller = new SessionController(mockSessionService); //Act var result = await controller.UpdateSession(updatedSession); //Assert Assert.AreEqual(typeof(NotFoundObjectResult), result.GetType()); }
public async Task <ServiceResponse <GetSessionDto> > UpdateSession(UpdateSessionDto updatedSession) { ServiceResponse <GetSessionDto> serviceResponse = new ServiceResponse <GetSessionDto>(); try { Session session = await _context.Sessions.Include(s => s.User) .FirstOrDefaultAsync(s => s.Id == updatedSession.Id); if (session != null && session.User.Id == GetUserId()) { double result = updatedSession.Duration; if (result > 0) { session.StartDateTime = updatedSession.StartDateTime; session.EndDateTime = updatedSession.EndDateTime; _context.Sessions.Update(session); await _context.SaveChangesAsync(); serviceResponse.Data = _mapper.Map <GetSessionDto>(session); } else if (result <= 0) { serviceResponse.Success = false; serviceResponse.Message = "EndDateTime cannot be earlier than StartDateTime."; } } else { serviceResponse.Success = false; serviceResponse.Message = "Session not found."; } } catch (Exception ex) { serviceResponse.Success = false; serviceResponse.Message = ex.Message; } return(serviceResponse); }
public async Task UpdateSession_HappyPath_ShouldReturnBadRequest() { //Arrange var updatedSession = new UpdateSessionDto(); var mockSessionService = A.Fake <ISessionService>(); A.CallTo(() => mockSessionService.UpdateSession(updatedSession)).Returns( new ServiceResponse <GetSessionDto> { Message = "EndDateTime cannot be earlier than StartDateTime." }); var controller = new SessionController(mockSessionService); //Act var result = await controller.UpdateSession(updatedSession); var badResult = result as BadRequestObjectResult; var actualResult = badResult.Value as string; //Assert Assert.AreEqual(typeof(BadRequestObjectResult), result.GetType()); }
public IActionResult Update([FromForm] UpdateSessionDto dto) { _service.Update(dto); return(Ok(GetResponse())); }
private static (bool, string) ValidateAndTryParse(Guid userId, string sessionIdString, out Guid sessionId, UpdateSessionDto data) { var valid = true; var message = new List <string>(); // Validate user id if (userId == Guid.Empty) { valid = false; message.Add("User ID is invalid."); } // Validate and try parse session id if (!Guid.TryParse(sessionIdString, out sessionId)) { valid = false; message.Add("Session ID is invalid."); } // Validate data if (string.IsNullOrWhiteSpace(data.SessionName)) { valid = false; message.Add("Session name is missing."); } return(valid, string.Join("\n", message)); }
public static async Task <HttpResponseData> RunAsync( [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "sessions/{sessionIdString}")] HttpRequestData req, string sessionIdString, UpdateSessionDto data ) { // Create response var response = req.CreateResponse(); // Get user id var userId = Utils.GetUserId(req.Headers); // Validate params Guid sessionId; var valid = ValidateAndTryParse(userId, sessionIdString, out sessionId, data); if (valid.Item1) { try { // Get container var sessionContainer = await CosmosDb.GetContainerAsync(CosmosDb.SESSION_CONTAINER_ID); // Get item var query = new QueryDefinition(@$ "SELECT * FROM c WHERE c.userId = @userId AND c.id = @sessionId") .WithParameter("@userId", userId) .WithParameter("@sessionId", sessionId); var session = await CosmosDb.GetItemByQueryAsync <Session>(sessionContainer, query); if (session != null) { // Update item session.SessionName = data.SessionName; session.Note = data.Note; Task.WaitAll( // Save to database CosmosDb.UpdateItemAsync <Session>(sessionContainer, session, session.Id.ToString(), session.UserId.ToString()), // Response update successful response.WriteAsJsonAsync("Updated session").AsTask() ); } else { // Response not found message await response.WriteAsJsonAsync("Session not found"); } } catch (CosmosException ex) { // Response error message await response.WriteAsJsonAsync(ex.Message); // Set http status code response.StatusCode = (HttpStatusCode)ex.Status; } } else { // Response invalid await response.WriteAsJsonAsync(valid.Item2); // Set http status code response.StatusCode = HttpStatusCode.BadRequest; } return(response); }