public async Task <bool> UpdateVolunteerAvailability(IFormCollection formData, VolunteerProfile vol, FoodBankContext _context) { await _context.Entry(vol).Collection(p => p.Availabilities).LoadAsync(); List <Availability> oldAvailabilities = await _context.Availabilities.Where(a => a.Volunteer.Id == vol.Id).ToListAsync(); _context.Availabilities.RemoveRange(oldAvailabilities); await _context.SaveChangesAsync(); List <Availability> newAvailabilites = GetAvailabilitiesFromFormData(formData, vol); if (newAvailabilites != null) { await _context.Availabilities.AddRangeAsync(newAvailabilites); await _context.SaveChangesAsync(); return(true); } else { await _context.Availabilities.AddRangeAsync(oldAvailabilities); await _context.SaveChangesAsync(); return(false); } }
public async Task <IActionResult> PutFoodItem([FromRoute] int id, [FromBody] FoodItem foodItem) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != foodItem.Id) { return(BadRequest()); } _context.Entry(foodItem).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FoodItemExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
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 }); } }
private async Task CreateSingleShift(Shift newShift) { string positionName = (await GetPositionsWithoutAll()).FirstOrDefault(p => p.Id == newShift.PositionId).Name; var chosenVolunteer = newShift.VolunteerProfileId != null || newShift.VolunteerProfileId > 0 ? _context.VolunteerProfiles .Where(p => p.Id == newShift.VolunteerProfileId).FirstOrDefault() : null; string volunteerName = chosenVolunteer.FirstName + " " + chosenVolunteer.LastName; newShift.Subject = newShift.VolunteerProfileId == null ? $"Open - {positionName}" : $"{volunteerName} - {positionName}"; _context.Shifts.Add(newShift); await _context.SaveChangesAsync(); }
public async Task CreateShift_ReportsSuccessAndPopulatesDB_IfSingleShiftCreationWasSuccessful() { var calendarService = GetCalendarService(); var insertParams = new Dictionary <string, object>(); var newShift = new ShiftReadEditDto() { StartTime = DateTime.Now.AddHours(6), EndTime = DateTime.Now.AddHours(12), PositionId = 3 }; string result = await calendarService.CreateShift(newShift, insertParams); var shifts = (await calendarService.GetShifts()); Assert.Equal(DateTime.Now.AddHours(6), shifts[0].StartTime); Assert.Equal(DateTime.Now.AddHours(12), shifts[0].EndTime); Assert.Equal(3, shifts[0].PositionId); Assert.Equal("A new shift has successfully been created", result); var testServiceProvider = new TestServiceProvider(); var config = testServiceProvider.GetConfig(); var contextOptionsBuilder = new DbContextOptionsBuilder <FoodBankContext>() .UseMySql(config.GetConnectionString("MainDevConnection")); var context = new FoodBankContext(contextOptionsBuilder.Options); using (context) { foreach (var shift in shifts) { var currentShift = await context.Shifts.FirstOrDefaultAsync(s => s.Id == shift.Id); context.Remove(currentShift); } await context.SaveChangesAsync(); } }