public void UpdateHighlight_update_highlight_and_return_trueAsJson() { var project = insertAndgetProject(getRandomProject()); var requirement = insertAndgetRequirement(getRandomRequirement(project)); var taskList = insertTasksAndgetList(requirement, 1); var task = taskList.OrderBy(x => x.Id).FirstOrDefault(); var oldHighlight = insertAndGetHighlight(getRandomHighlight(task)); var randonColor = r.Next(0, 4); var newHighLight = new DTOHighlightColor { TaskId = oldHighlight.TaskId, Id = oldHighlight.Id, Color = randonColor }; // Arrange var taskController = Container.createTasksController(); // Act var result = taskController.WriteHighlight(newHighLight) as JsonResult; // Assert var resultAsInt = result.Data as int?; var expectedNewHighlight = Container.createIDbContext().Highlights.Where(x => x.TaskId == newHighLight.TaskId).ToList().FirstOrDefault(); Assert.IsTrue(result != null && resultAsInt.Value > 0, "It is not returning a json result"); Assert.IsTrue(expectedNewHighlight != null, "The record does not exist."); Assert.IsTrue ( (int)newHighLight.Color == expectedNewHighlight.Color, "The record was not updated." ); }
public void InsertHighlight_insert_highlight_and_return_idAsJson() { var project = insertAndgetProject(getRandomProject()); var requirement = insertAndgetRequirement(getRandomRequirement(project)); var taskList = insertTasksAndgetList(requirement, 3); var taskId = taskList.OrderBy(x => x.Id).FirstOrDefault().Id; var randonColor = r.Next(0, 4); DTOHighlightColor highlight = new DTOHighlightColor { Color = randonColor, TaskId = taskId, }; // Arrange var taskController = Container.createTasksController(); // Act var result = taskController.WriteHighlight(highlight) as JsonResult; // Assert var resultAsInt = result.Data as int?; var expectedHighlight = Container.createIDbContext().Highlights.Where(x => x.Id == resultAsInt).ToList().FirstOrDefault(); Assert.IsTrue(result != null && resultAsInt > 0, "It is not returning a json result"); Assert.IsTrue(expectedHighlight != null, "The record was not inserted."); Assert.IsTrue ( highlight.Color == expectedHighlight.Color && highlight.TaskId == expectedHighlight.TaskId, "The record inserted does not correspond with the one created." ); }
public DTOHighlightColor insertAndGetHighlight(DTOHighlightColor highlight) { int id = Container.createITaskRepository().insertHighlightColorAndSaveChanges(Container.createIDbContext(), highlight); highlight.Id = id; return(highlight); }
// Highlight ================================================ /// <summary> /// Check if highlight exist. /// </summary> /// <param name="context">Context of the database.</param> /// <param name="highlight">Highlight to check for existence</param> /// <returns>True if highlight exist. Otherwise false.</returns> public bool highlightExist(IDbContext context, DTOHighlightColor highlight) { var query = getAll <Highlight>(context) .Where(x => x.TaskId == highlight.TaskId) .Any(); return(query); }
/// <summary> /// Insert a color for a task and save changes. /// </summary> /// <param name="context">Context of the database.</param> /// <param name="highlight">Highlight DTO to be inserted.</param> /// <returns>The id of the inserted record.</returns> public int insertHighlightColorAndSaveChanges(IDbContext context, DTOHighlightColor highlight) { // Throw exception if fk is invalid throwExceptionIfForeignKeyInvalid(highlight.TaskId); // Get entity from DTO var entity = Mapper.mapEntityHighlight(highlight); return(insertAndSaveChanges <Highlight>(context, entity)); }
// Highlight public DTOHighlightColor getRandomHighlight(DTOTask task) { var randonColor = r.Next(0, 4); DTOHighlightColor highlight = new DTOHighlightColor { Color = randonColor, TaskId = task.Id }; return(highlight); }
/// <summary> /// Update a color for a task. /// </summary> /// <param name="context">Context of the database.</param> /// <param name="highlight">Highlight DTO to be updated.</param> public void updateHighlightColor(IDbContext context, DTOHighlightColor highlight) { // Get old entity var oldEntity = context.Highlights.Where(x => x.Id == highlight.Id).FirstOrDefault(); // Get new entity var newEntity = Mapper.mapEntityHighlight(highlight); // Update changes update <Highlight>(context, newEntity, oldEntity); }
// Highlight =========================================== /// <summary> /// Insert or update a DTO highlight. /// </summary> /// <param name="highlight">A DTO Highlight to insert or update.</param> public int writeHighlight(DTOHighlightColor highlight) { using (DbContext) { int ret; // True if the highlight already exist in db. Otherwise false bool highlightExist = taskRepository.highlightExist(DbContext, highlight); // Update highlight if it already exist. Insert it if it does not. if (highlightExist) { taskRepository.updateHighlightColor(DbContext, highlight); DbContext.SaveChanges(); ret = highlight.Id; } else { ret = taskRepository.insertHighlightColorAndSaveChanges(DbContext, highlight); } return(ret); } }
/// <summary> /// Delete a highlight entity. /// </summary> /// <param name="context">Context of the database.</param> /// <param name="highlight">Highlight DTO to be deleted</param> public void deleteHighlightColor(IDbContext context, DTOHighlightColor highlight) { // Delete entity delete <Highlight>(context, Mapper.mapEntityHighlight(highlight)); }
public ActionResult WriteHighlight(DTOHighlightColor highlight) { int id = taskService.writeHighlight(highlight); return(Json(id)); }
/// <summary> /// Map Entity Highlight to DTO Highlight. /// </summary> /// <param name="DTOHighlight">Highlight DTO to map from.</param> /// <returns>A Entity Highlight.</returns> public Highlight mapEntityHighlight(DTOHighlightColor DTOHighlight) { return(map <Highlight>(DTOHighlight)); }