public async Task <IActionResult> ChangeValidation([FromBody] BookingValidationDeserializer bookingValidationDsl, [FromRoute] bool validation) { Booking booking = await BookingServices.GetByIdsAsync(bookingValidationDsl.UserId, bookingValidationDsl.EventId, BookingServices.WithUserAndEvent(Context.Booking)); if (booking == null) { return(NotFound("Booking not found")); } booking.Present = validation; if (!validation && booking.Event.JuryPoint.HasValue) { BookingServices.RemoveJuryPoints( BookingServices.GetJuryPoint(bookingValidationDsl.UserId, (float)booking.Event.JuryPoint) ); } else if (booking.Event.JuryPoint.HasValue) { BookingServices.CreateJuryPoint((float)booking.Event.JuryPoint, bookingValidationDsl.UserId); EmailServices.SendFor(booking.User, booking.Event, BookingTemplate.PRESENT); } try { Context.Booking.Update(booking); await Context.SaveChangesAsync(); } catch (DbUpdateException ex) { return(BadRequest(ex)); } return(NoContent()); }
public async Task <IActionResult> ValidatePresence( [FromBody] BookingValidationDeserializer bookingValidation, [FromServices] IUserServices userServices) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!CurrentUser.Role.Name.Equals("Admin") && !userServices.IsModeratorFor(bookingValidation.EventId, CurrentUser.Id)) { return(BadRequest("Not Allowed to moderate this event")); } var query = BookingServices.WithUserAndEvent(Context.Booking); Booking book = await BookingServices.GetByIdsAsync(bookingValidation.UserId, bookingValidation.EventId, query); if (book == null) { return(NotFound("Booking not found")); } else if (!book.Event.OnGoingWindow() || !book.Event.Status.Equals(Status.ONGOING)) { return(BadRequest("Can't validate presence outside of open window")); } else if (book.Present) { return(BadRequest("Presence Already validated")); } else if (book.Event.ValidationRequired && (bool)!book.Validated) { return(BadRequest("User hasn't been validated")); } // VALIDATE THE PRESENCE book.Present = true; // CREATE THE PointJury ASSOCIATED if (book.Event.JuryPoint != null) { BookingServices.CreateJuryPoint((float)book.Event.JuryPoint, bookingValidation.UserId); } try { Context.Booking.Update(book); await Context.SaveChangesAsync(); EmailServices.SendFor(book.User, book.Event, BookingTemplate.PRESENT); } catch (DbUpdateException e) { return(BadRequest(e.InnerException.Message)); } return(NoContent()); }
private void MakeRequest(BookingValidationDeserializer dsl, User user, out StringContent content) { string json = JsonConvert.SerializeObject(dsl); content = new StringContent(json, Encoding.UTF8, "application/json"); HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", TokenBearerHelper.GetTokenFor(user, Context)); }
public async void ShouldValidateWithoutModeratorRoles(string email) { CreateFixture(email, false, out var @event, out var user, out var book); var deserializer = new BookingValidationDeserializer() { EventId = @event.Id, UserId = user.Id }; MakeRequest(deserializer, user, out var content); HttpResponseMessage body = await HttpClient.PostAsync("api/booking/validate", content); Assert.Equal("", body.Content.ReadAsStringAsync().Result); Assert.Equal(HttpStatusCode.NoContent, body.StatusCode); }
public async void ShouldAddJuryPoints_WhenUserHasBeenAdmited(string email) { CreateFixture(email, false, out var @event, out var user, out var book); var deserializer = new BookingValidationDeserializer() { EventId = @event.Id, UserId = user.Id }; MakeRequest(deserializer, user, out var content); HttpResponseMessage body = await HttpClient.PutAsync($"api/booking/change-validation/{true}", content); Assert.Equal("", body.Content.ReadAsStringAsync().Result); Assert.Equal(HttpStatusCode.NoContent, body.StatusCode); var bookAssertion = Context.JuryPoints.Where(e => e.UserId == user.Id).ToArray(); Assert.True(bookAssertion.Any(a => a.Points.Equals(1345.13F))); }
public async void ShouldInvalidUser_WhenHasBeenSetAsPresent(string email) { CreateFixture(email, true, out var @event, out var user, out var book); var deserializer = new BookingValidationDeserializer() { EventId = @event.Id, UserId = user.Id }; MakeRequest(deserializer, user, out var content); HttpResponseMessage body = await HttpClient.PutAsync($"api/booking/change-validation/{false}", content); Assert.Equal("", body.Content.ReadAsStringAsync().Result); Assert.Equal(HttpStatusCode.NoContent, body.StatusCode); var bookAssertion = Context.Booking.AsNoTracking() .FirstOrDefault(e => e.UserId == user.Id && e.EventId == @event.Id); Assert.Equal(false, bookAssertion.Present); }