public async Task <PunchClockResult> PunchClock(int userId, int position) { try { var volunteer = await _context.VolunteerProfiles.FirstOrDefaultAsync(v => v.UserID == userId); if (volunteer.ApprovalStatus != ApprovalStatus.Approved) { return(new PunchClockResult { Message = $"Your application must be approved before you can clock in.", Success = false }); } var existingClockIn = await _context.ClockedTime.FirstOrDefaultAsync(ct => ct.Volunteer.UserID == userId && ct.EndTime == null); if (existingClockIn != null) { _context.Update(existingClockIn); existingClockIn.EndTime = DateTime.UtcNow.AddHours(-6); await _context.SaveChangesAsync(); return(new PunchClockResult { Message = $"Goodbye {existingClockIn.Volunteer.FirstName}! You have successfully clocked out.", Success = true }); } else { ClockedTime newClockIn = new ClockedTime() { Volunteer = volunteer, StartTime = DateTime.UtcNow.AddHours(-6), Position = await _context.Positions.FirstOrDefaultAsync(p => p.Id == position) }; await _context.AddAsync(newClockIn); await _context.SaveChangesAsync(); return(new PunchClockResult { Message = $"Welcome {newClockIn.Volunteer.FirstName}! You have successfully clocked in.", Success = true }); } } catch (Exception ex) { Console.WriteLine(ex.Message); return(new PunchClockResult { Message = $"Something went wrong, please try again.", Success = false }); } }
public async Task <IActionResult> OnPostAddEntry() { var volunteerName = VolunteerNameForAdd; var volunteer = await _context.VolunteerProfiles.FirstOrDefaultAsync(p => p.FullNameWithID == VolunteerNameForAdd); var position = await _context.Positions.FirstOrDefaultAsync(p => p.Name == PositionNameForAdd); ClockedTime clock = new ClockedTime() { Position = position, Volunteer = volunteer, StartTime = EntryStartDate, EndTime = EntryEndDate }; _context.ClockedTime.Add(clock); await _context.SaveChangesAsync(); return(RedirectToPage(new { statusMessage = $"A new entry has been successfully added." })); }
public async Task <IActionResult> OnPostAddEntry() { string volunteerIdStr = VolunteerNameForAdd.Split(' ')[0]; int volunteerId = 0; if (int.TryParse(volunteerIdStr, out int volIdParsed)) { volunteerId = volIdParsed; } else { // return error } if (!Positions.Any(p => p.Name == PositionNameForAdd)) { // return error } var volunteer = await _context.VolunteerProfiles.FirstOrDefaultAsync(p => p.Id == volunteerId); var position = await _context.Positions.FirstOrDefaultAsync(p => p.Name == PositionNameForAdd); var start = EntryStartDate + EntryStartTime; var end = EntryEndDate + EntryEndTime; ClockedTime clock = new ClockedTime() { Position = position, Volunteer = volunteer, StartTime = start, EndTime = end }; _context.ClockedTime.Add(clock); await _context.SaveChangesAsync(); return(RedirectToPage(new { statusMessage = $"A new entry has been successfully added." })); }