public IActionResult CreateNewTimeSheet([FromBody] List <TimeSheetDataEntryDTO> scheduleList) { TimeSheetSchedule schedule = new TimeSheetSchedule(); int userId = int.Parse(User.FindFirst("userid").Value);//Get current user id value DateTime currentDateTime = _appDateTimeService.GetCurrentDateTime(); var newTimeSheet = new TimeSheet(); newTimeSheet.CreatedAt = currentDateTime; newTimeSheet.UpdatedAt = currentDateTime; newTimeSheet.InstructorId = userId; newTimeSheet.YearAndMonth = new DateTime(currentDateTime.Year, currentDateTime.Month, 1); newTimeSheet.UpdatedById = userId; newTimeSheet.CreatedById = userId; newTimeSheet.TimeSheetSchedules = new List <TimeSheetSchedule>(); foreach (var oneData in scheduleList) { schedule = new TimeSheetSchedule(); schedule.CustomerAccountName = oneData.customerAccountName; schedule.DateOfLesson = DateTime.ParseExact(oneData.dateOfLesson, "dd/MM/yyyy", CultureInfo.InvariantCulture); schedule.OfficialStartTimeInMinutes = oneData.officialStartTimeInMinutes; schedule.OfficialEndTimeInMinutes = oneData.officialEndTimeInMinutes; schedule.OfficialStartTimeInHHMM = oneData.officialStartTimeInHHMM; schedule.OfficialEndTimeInHHMM = oneData.officialEndTimeInHHMM; schedule.RatePerHour = oneData.ratePerHour; schedule.WageRatePerHour = oneData.wageRatePerHour; schedule.IsReplacementInstructor = false; schedule.IsSystemCreated = true; schedule.LessonTypeNames = ""; schedule.CreatedAt = currentDateTime; schedule.UpdatedAt = currentDateTime; schedule.CreatedById = userId; schedule.UpdatedById = userId; schedule.Status = "NOT UPDATED"; //NOT UPDATED, UPDATED, COMPLETED newTimeSheet.TimeSheetSchedules.Add(schedule); }//for try { Database.TimeSheets.Add(newTimeSheet); Database.SaveChanges(); } catch (Exception ex) { string customMessage = ""; if (ex.InnerException.Message.Contains("TimeSheet_UniqueConstraint") == true) { customMessage = "Timesheet for " + newTimeSheet.YearAndMonth.Month + "/" + newTimeSheet.YearAndMonth.Year + " already exists."; object httpFailRequestResultMessage = new { message = customMessage }; //Return a bad http request message to the client return(BadRequest(httpFailRequestResultMessage)); } else { return(BadRequest(ex.InnerException.Message)); } } return(Ok(new { message = "Created time sheet" })); }//CreateNewTimeSheet
public JsonResult GetOneTimeSheetSchedule(int id) { object result = null; TimeSheetSchedule oneTimeSheetSchedule = null; //The method logic uses the current login user id to obtain the time sheet (based on the given year and month) int userId = int.Parse(User.FindFirst("userid").Value); //Get current user id value oneTimeSheetSchedule = Database.TimeSheetSchedules.Where(timeSheetSchedule => timeSheetSchedule.TimeSheetScheduleId == id) .Include(timeSheetSchedule => timeSheetSchedule.TimeSheet).SingleOrDefault(); TimeSheetScheduleSignature oneTimeSheetScheduleSignature = Database.TimeSheetScheduleSignature.Where(signature => signature.TimeSheetScheduleId == id).SingleOrDefault(); if (oneTimeSheetSchedule != null) { TimeSpan span = TimeSpan.FromMinutes(oneTimeSheetSchedule.OfficialStartTimeInMinutes); string startTimeInHHMM = span.ToString(@"hh\:mm"); span = TimeSpan.FromMinutes(oneTimeSheetSchedule.OfficialEndTimeInMinutes); string endTimeInHHMM = span.ToString(@"hh\:mm"); byte[] signatureImage = new byte[] { }; if (oneTimeSheetScheduleSignature != null) { signatureImage = oneTimeSheetScheduleSignature.Signature; } //The properties applied in this anonymous object matches the needs of the front-end's update timesheet schedule logic. result = (new { id = oneTimeSheetSchedule.TimeSheetScheduleId, title = oneTimeSheetSchedule.OfficialStartTimeInHHMM + '~' + oneTimeSheetSchedule.OfficialEndTimeInHHMM + ' ' + oneTimeSheetSchedule.CustomerAccountName, customerAccountName = oneTimeSheetSchedule.CustomerAccountName, officialStartTimeHHMM = startTimeInHHMM, officialEndTimeHHMM = endTimeInHHMM, officialStartTimeInMinutes = oneTimeSheetSchedule.OfficialStartTimeInMinutes, officialEndTimeInMinutes = oneTimeSheetSchedule.OfficialEndTimeInMinutes, actualStartTimeInMinutes = oneTimeSheetSchedule.ActualStartTimeInMinutes, actualEndTimeInMinutes = oneTimeSheetSchedule.ActualEndTimeInMinutes, lessonTypeNames = oneTimeSheetSchedule.LessonTypeNames, status = oneTimeSheetSchedule.Status, isSystemCreated = oneTimeSheetSchedule.IsSystemCreated, isReplacementInstructor = oneTimeSheetSchedule.IsReplacementInstructor, dateOfLesson = oneTimeSheetSchedule.DateOfLesson, ratePerHour = oneTimeSheetSchedule.RatePerHour, signature = signatureImage }); } return(new JsonResult(result)); }//GetOneTimeSheetSchedule