public async Task <ActionResult <Ticket> > CreateTicketAsync(Ticket model) { if (model.Id != Guid.Empty) { return(BadRequest( new ProblemDetails { Detail = "This method can't be used to update tickets." })); } if (await _context.Tickets.AnyAsync(t => t.TicketNumber == model.TicketNumber)) { return(BadRequest( new ProblemDetails { Detail = $"The ticket number \"{model.TicketNumber}\" is already in use." })); } var evt = await _context.Events.FindAsync(model.EventId); if (evt == null) { return(BadRequest( new ProblemDetails { Detail = $"There's no event with id {model.EventId}." })); } var entity = new ApplicationCore.Models.Ticket(); _mapper.Map(model, entity); entity.TicketSecret = Guid.NewGuid().ToString("N"); entity.TicketNumber = entity.TicketNumber ?? _ticketNumberService.GenerateTicketNumber(evt); SetAuthorInfo(entity); _context.Add(entity); _context.SaveChanges(); _context.Entry(entity).Reference(e => e.TicketType).Load(); if (entity.BookingDate != null) { await _auditEventLog.AddAsync(new ApplicationCore.Models.AuditEvent { Time = entity.BookingDate.Value, TicketId = entity.Id, Action = EventManagementConstants.Auditing.Actions.TicketOrder, Detail = $"Ticket der Kategorie \"{entity.TicketType.Name}\" wurde für {entity.TicketType.Price:c} bestellt." }); } model = _mapper.Map <Ticket>(entity); return(CreatedAtAction(nameof(GetById), new { id = model.Id }, model)); }
public JsonResult Subscribe(string id) { using (var context = new EventsDbContext()) { var currentUser = context.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault(); var userToSubscribeTo = context.Users.Find(id); userToSubscribeTo.Followers.Add(currentUser); currentUser.Following.Add(userToSubscribeTo); context.Entry(currentUser).State = System.Data.Entity.EntityState.Modified; context.Entry(userToSubscribeTo).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } return Json(new { Operation = "successful" }, JsonRequestBehavior.AllowGet); }
/// <summary> /// HTTP GET endpoint for "/GuestBookings/Attend/<paramref name="id"/>?customerId=<paramref name="customerId"/>". <para/> /// Marks attendance for a <see cref="Customer"/> whose Id is <paramref name="customerId"/> on the <see cref="Event"/> /// whose Id is <paramref name="id"/>. /// </summary> /// <param name="id">The <see cref="Event"/>'s Id.</param> /// <param name="customerId">The <see cref="Customer"/>'s Id.</param> /// <returns>Directs the user to the <see cref="Index(int?)"/> view with the filter of <paramref name="id"/>.</returns> public async Task <IActionResult> Attend(int?id, int?customerId) { if (id == null || customerId == null) { return(NotFound()); } GuestBooking booking = await _context.Guests.FindAsync(customerId, id); if (booking == null) { return(NotFound()); } if (booking.Attended == true) // Already attended { return(RedirectToAction(nameof(Index), new { id })); } booking.Attended = true; // Attend _context.Entry(booking).State = EntityState.Modified; await _context.SaveChangesAsync(); if (StringValues.IsNullOrEmpty(Request.Headers["Referer"])) { return(RedirectToAction(nameof(Index))); } return(Redirect(Request.Headers["Referer"].ToString())); }
public ActionResult <IList <TicketType> > AddOrUpdateTicketTypes(Guid eventId, [FromBody] TicketType[] items) { var evt = _context.Events .Include(e => e.TicketTypes) .SingleOrDefault(e => e.Id == eventId); if (evt == null) { return(NotFound()); } // Delete ticket types. foreach (ApplicationCore.Models.TicketType ticketType in evt.TicketTypes) { if (items.All(t => t.Id != ticketType.Id)) { _context.Entry(ticketType).State = EntityState.Deleted; } } var list = new List <TicketType>(); foreach (TicketType item in items) { if (item.Id != Guid.Empty) { // Update ticket type. var entity = evt.TicketTypes.SingleOrDefault(t => t.Id == item.Id); if (entity == null) { return(BadRequest( new ProblemDetails { Detail = $"There is no ticket type with id {item.Id}." })); } else { _mapper.Map(item, entity); _context.SaveChanges(); list.Add(_mapper.Map <TicketType>(entity)); } } else { // Create new ticket type. var entity = _mapper.Map <ApplicationCore.Models.TicketType>(item); evt.TicketTypes.Add(entity); _context.SaveChanges(); list.Add(_mapper.Map <TicketType>(entity)); } } _context.SaveChanges(); return(list); }
public ActionResult UpdateProfile(UserViewModel userViewModel) { var userUpdated = new User(); using (var context = new EventsDbContext()) { var currentUser = context.Users.Find(userViewModel.Id); userUpdated = ViewModelMapper.MapUserViewModelToUser(currentUser, userViewModel); context.Entry(userUpdated).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } return Redirect("http://localhost:15716/Users/MyProfile"); }
public IEntity Add(IEntity entity) { _context.Entry(entity).State = EntityState.Added; _context.SaveChanges(); return(entity); }