public async Task <IHttpActionResult> Delete(Guid id) { using (var context = new IdentoolsDbContext()) { var suggestion = await context.Suggestions.FindAsync(id); context.Suggestions.Remove(suggestion); await context.SaveChangesAsync(); } return(Ok()); }
public async Task <IHttpActionResult> Post([FromBody] Suggestion suggestion) { var newSuggestion = new Suggestion { Location = suggestion.Location, StartTime = suggestion.StartTime, UserName = HttpContext.Current.User.Identity.Name }; using (var context = new IdentoolsDbContext()) { var existingAttendance = await context.SuggestionAttendees.SingleOrDefaultAsync(sa => sa.Suggestion.StartTime.Day == DateTime.Now.Day && sa.UserName == HttpContext.Current.User.Identity.Name); Guid?exisitingAttendanceSuggestionId = null; if (existingAttendance != null) { exisitingAttendanceSuggestionId = existingAttendance.SuggestionId; context.SuggestionAttendees.Remove(existingAttendance); } newSuggestion.SuggestionAttendees.Add(new SuggestionAttendee { UserName = HttpContext.Current.User.Identity.Name }); context.Suggestions.Add(newSuggestion); await context.SaveChangesAsync(); if (exisitingAttendanceSuggestionId.HasValue) { SuggestionHub.UpdateSuggestion(exisitingAttendanceSuggestionId.Value); } } var suggestionCardModel = Mapper.Map <SuggestionCardModel>(newSuggestion); SuggestionHub.AddSuggestion(suggestionCardModel); return(Ok()); }
public async Task <IHttpActionResult> Attend(Guid id) { using (var context = new IdentoolsDbContext()) { try { var username = HttpContext.Current.User.Identity.Name; var existingAttendance = await context.SuggestionAttendees.Include(sa => sa.Suggestion).SingleOrDefaultAsync(sa => sa.Suggestion.StartTime.Day == DateTime.Now.Day && sa.UserName == username); if (existingAttendance == null) { var suggestionAttendee = new SuggestionAttendee { SuggestionId = id, UserName = username }; context.SuggestionAttendees.Add(suggestionAttendee); await context.SaveChangesAsync(); SuggestionHub.UpdateSuggestion(id); var suggestion = context.Suggestions.Find(id); SuggestionHub.AnnounceAttendance(username.Split('\\').Last(), suggestion.Location, true); } else { if (existingAttendance.SuggestionId == id) { context.SuggestionAttendees.Remove(existingAttendance); await context.SaveChangesAsync(); var suggestion = context.Suggestions.Find(id); SuggestionHub.AnnounceAttendance(username.Split('\\').Last(), suggestion.Location, false); } else { var existingAttendanceLocation = existingAttendance.Suggestion.Location; context.SuggestionAttendees.Remove(existingAttendance); SuggestionHub.AnnounceAttendance(username.Split('\\').Last(), existingAttendanceLocation, false); var suggestionAttendee = new SuggestionAttendee { SuggestionId = id, UserName = username }; context.SuggestionAttendees.Add(suggestionAttendee); await context.SaveChangesAsync(); SuggestionHub.UpdateSuggestion(id); SuggestionHub.AnnounceAttendance(username.Split('\\').Last(), existingAttendanceLocation, true); } SuggestionHub.UpdateSuggestion(existingAttendance.SuggestionId); } } catch (Exception ex) { return(InternalServerError(ex)); } } return(Ok()); }