/// <summary> /// Gets the timesheet model to be inserted in database. /// </summary> /// <param name="timesheetDate">The timesheet date to be save.</param> /// <param name="timesheetViewModel">The timesheet view model.</param> /// <param name="userObjectId">The logged-in user object Id.</param> /// <returns>The timesheet entity model.</returns> public TimesheetEntity MapForCreateModel(DateTime timesheetDate, TimesheetDetails timesheetViewModel, Guid userObjectId) { timesheetViewModel = timesheetViewModel ?? throw new ArgumentNullException(nameof(timesheetViewModel)); var timesheet = new TimesheetEntity { TaskId = timesheetViewModel.TaskId, TaskTitle = timesheetViewModel.TaskTitle, TimesheetDate = timesheetDate, Hours = timesheetViewModel.Hours, Status = timesheetViewModel.Status, UserId = userObjectId, }; if (timesheetViewModel.Status == (int)TimesheetStatus.Submitted) { timesheet.SubmittedOn = DateTime.UtcNow; } else { timesheet.SubmittedOn = null; } return(timesheet); }
public async Task AddCustomTaskForMemberAsync_WithInvalidProject_ReturnsStatusBadRequest() { var projectId = Guid.NewGuid(); this.repositoryAccessors.Setup(ra => ra.ProjectRepository).Returns(() => this.projectRepository.Object); this.projectRepository. Setup(projectRepository => projectRepository.GetAsync(It.IsAny <Guid>())). Returns(Task.FromResult(TestData.Projects.Where(project => project.Id == projectId).FirstOrDefault())); this.memberRepository. Setup(memberRepository => memberRepository.GetAllActiveMembersAsync(It.IsAny <Guid>())). Returns(Task.FromResult(TestData.InvalidMembers)); var timesheetDetails = new TimesheetDetails { EndDate = new DateTime(DateTime.UtcNow.Year, 1, 2), Hours = 3, IsAddedByMember = true, StartDate = new DateTime(DateTime.UtcNow.Year, 1, 2), TaskTitle = "New task", }; var taskHelper = new TaskHelper(this.repositoryAccessors.Object, new Mock <ILogger <TaskHelper> >().Object); var addResult = (ObjectResult)await this.projectController.AddCustomTaskForMemberAsync(projectId, timesheetDetails); var error = (ErrorResponse)addResult.Value; Assert.AreEqual(StatusCodes.Status400BadRequest, addResult.StatusCode); Assert.AreEqual("Invalid project", error.Message); }
public IActionResult Post([FromBody] TimesheetDetails m2) { db.timesheetDetails.Add(m2); db.SaveChanges(); return(StatusCode(StatusCodes.Status201Created)); }
/// <summary> /// Maps timesheet view model details to timesheet entity model that to be updated in database. /// </summary> /// <param name="timesheetViewModel">The timesheet entity view model.</param> /// <param name="timesheetModel">The timesheet entity model.</param> public void MapForUpdateModel(TimesheetDetails timesheetViewModel, TimesheetEntity timesheetModel) { timesheetViewModel = timesheetViewModel ?? throw new ArgumentNullException(nameof(timesheetViewModel)); timesheetModel = timesheetModel ?? throw new ArgumentNullException(nameof(timesheetModel)); timesheetModel.Status = timesheetViewModel.Status; timesheetModel.Hours = timesheetViewModel.Hours; timesheetModel.LastModifiedOn = DateTime.UtcNow; }
/// <summary> /// Gets the task model to be inserted in database. /// </summary> /// <param name="timesheetViewModel">The timesheet view model.</param> /// <param name="projectId">The project Id.</param> /// <param name="userObjectId">The logged-in user object Id.</param> /// <returns>Returns task entity model.</returns> public TaskEntity MapForCreateModel(TimesheetDetails timesheetViewModel, Guid projectId, Guid userObjectId) { timesheetViewModel = timesheetViewModel ?? throw new ArgumentNullException(nameof(timesheetViewModel)); var task = new TaskEntity { Title = timesheetViewModel.TaskTitle, IsAddedByMember = timesheetViewModel.IsAddedByMember, StartDate = timesheetViewModel.StartDate.Date, EndDate = timesheetViewModel.EndDate.Date, ProjectId = projectId, }; return(task); }
public async Task <IActionResult> AddCustomTaskForMemberAsync(Guid projectId, [FromBody] TimesheetDetails timesheetDetails) { this.RecordEvent("Add task- The HTTP POST call to add new task has been initiated.", RequestType.Initiated); if (projectId == Guid.Empty) { this.RecordEvent("Add task- The HTTP POST call to add new task has been failed.", RequestType.Failed); return(this.BadRequest("The project Id must be provided in order to create a task.")); } if (timesheetDetails == null) { this.RecordEvent("Add task- The HTTP POST call to add new task has been failed.", RequestType.Failed); return(this.BadRequest("Task details were not provided.")); } try { var projectDetails = await this.repositoryAccessors.ProjectRepository.GetAsync(projectId); if (projectDetails == null) { this.logger.LogInformation("Project details not found"); return(this.BadRequest(new ErrorResponse { Message = "Invalid project" })); } if (timesheetDetails.StartDate < projectDetails.StartDate.Date || timesheetDetails.EndDate > projectDetails.EndDate.Date) { this.logger.LogInformation("Task start and end date is not within project start and end date"); return(this.BadRequest(new ErrorResponse { Message = "Invalid start and end date for task" })); } var taskDetails = this.taskMapper.MapForCreateModel(timesheetDetails, projectId, Guid.Parse(this.UserAadId)); var creationResult = await this.taskHelper.AddMemberTaskAsync(taskDetails, projectId, Guid.Parse(this.UserAadId)); if (creationResult != null) { this.RecordEvent("Add task- The HTTP POST call to add new task has been succeeded.", RequestType.Succeeded); var taskViewModel = this.taskMapper.MapForViewModel(creationResult); return(this.Ok(taskViewModel)); } this.RecordEvent("Add task- The HTTP POST call to add new task has been failed.", RequestType.Failed); return(this.StatusCode((int)HttpStatusCode.InternalServerError)); } catch (Exception ex) { this.RecordEvent("Add task- The HTTP POST call to add new task has been failed.", RequestType.Failed); this.logger.LogError(ex, "Error occurred while creating a new task."); throw; } }
public async Task <IActionResult> AddCustomTaskForMemberAsync(Guid projectId, [FromBody] TimesheetDetails timesheetDetails) { this.RecordEvent("Add task- The HTTP POST call to add new task has been initiated.", RequestType.Initiated); if (projectId == Guid.Empty) { this.RecordEvent("Add task- The HTTP POST call to add new task has been failed.", RequestType.Failed); return(this.BadRequest("The project Id must be provided in order to create a task.")); } if (timesheetDetails == null) { this.RecordEvent("Add task- The HTTP POST call to add new task has been failed.", RequestType.Failed); return(this.BadRequest("Task details were not provided.")); } try { var taskDetails = this.taskMapper.MapForCreateModel(timesheetDetails, projectId, Guid.Parse(this.UserAadId)); var creationResult = await this.taskHelper.AddMemberTaskAsync(taskDetails, projectId, Guid.Parse(this.UserAadId)); if (creationResult.StatusCode == HttpStatusCode.OK) { this.RecordEvent("Add task- The HTTP POST call to add new task has been succeeded.", RequestType.Succeeded); var taskViewModel = this.taskMapper.MapForViewModel(creationResult.Response as TaskEntity); return(this.Ok(taskViewModel)); } this.RecordEvent("Add task- The HTTP POST call to add new task has been failed.", RequestType.Failed); return(this.StatusCode((int)creationResult.StatusCode, creationResult.ErrorMessage)); } catch (Exception ex) { this.RecordEvent("Add task- The HTTP POST call to add new task has been failed.", RequestType.Failed); this.logger.LogError(ex, "Error occurred while creating a new task."); throw; } }