private IBaseRecord ParsePiecesAndTimeRecord(XElement recordElement) { IBaseRecord rec = null; Enum.TryParse(recordElement.Attribute("Type")?.Value, out ERecordType recType); XElement dataElement = recordElement.Element("Data"); if (dataElement != null) { DateTime date = ParseDate(dataElement.Attribute("Date")?.Value); string description = dataElement.Attribute("Description")?.Value; var(price, bonus) = ParseIRecord(recType, dataElement.Attribute("Price")?.Value, dataElement.Attribute("Bonus")?.Value); switch (recType) { case ERecordType.Hours: TimeSpan workTimeFrom = ParseTimeRecord(dataElement.Attribute("WorkTimeFrom")?.Value.Split(':')).ToTimeSpan(); TimeSpan workTimeTo = ParseTimeRecord(dataElement.Attribute("WorkTimeTo")?.Value.Split(':')).ToTimeSpan(); TimeSpan breakTime = ParseTimeRecord(dataElement.Attribute("BreakTime")?.Value.Split(':')) .ToTimeSpan(); WorkTime overTime = ParseTimeRecord(dataElement.Attribute("OverTime")?.Value.Split(':')); rec = new HoursRecord(date, workTimeFrom, workTimeTo, price, bonus, description, overTime, new WorkTime(breakTime.Hours, breakTime.Minutes)); break; case ERecordType.Pieces: uint.TryParse(dataElement.Attribute("Variable")?.Value, out uint pieces); rec = new PiecesRecord(date, pieces, price, bonus, description); break; case ERecordType.Vacation: rec = new VacationRecord(date, description); break; } } return(rec); }
public async Task <IActionResult> RemoveAssignedHoursToUserForProject([FromBody] HoursRecordForCreation hoursRecord) { User user = await userService.FindUser(hoursRecord.UserId); if (user == null) { ModelState.AddModelError("User", "The user is not found"); } Project project = await projectService.FindProject(hoursRecord.ProjectId); if (project == null) { ModelState.AddModelError("Project", "The project is not found"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { HoursRecord record = await hoursRecordService.RemoveAssignedHoursToUserForProject(hoursRecord); return(Created($"Get/{record.Id}", record)); } catch (Exception xcp) { return(StatusCode(500, xcp.InnerException.Message)); } }
public async Task <IActionResult> AddSpentHoursToUserForProject([FromBody] HoursRecordForCreation hoursRecord) { User user = await userService.FindUser(hoursRecord.UserId); if (user == null) { ModelState.AddModelError("User", "The user is not found"); } Project project = await projectService.FindProject(hoursRecord.ProjectId); if (project == null) { ModelState.AddModelError("Project", "The project is not found"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } int availableHours = await hoursRecordService.CheckAvailableHoursForUserOnProject(user, project); if (availableHours < hoursRecord.Hours) { ModelState.AddModelError("Hours", "There is no enough available hours for the user on the project. Please add him more assigned hours first."); } try { HoursRecord record = await hoursRecordService.AddSpentHoursToUserForProject(hoursRecord); return(Created($"Get/{record.Id}", record)); } catch (Exception xcp) { return(StatusCode(500, xcp.InnerException.Message)); } }
private async Task <IActionResult> Get(int id) { HoursRecord record = await hoursRecordService.GetRacord(id); if (record == null) { return(NotFound()); } return(Ok(record)); }