public T Add(T item) { var entity = context.Add <T>(item); context.SaveChanges(); return(entity.Entity); }
public ActionResult <Ticket> CreateTicket(Ticket model) { if (model.Id > 0) { return(BadRequest( new ProblemDetails { Detail = "This method can't be used to update tickets." })); } var evt = _context.Events.Find(model.EventId); if (evt == null) { return(BadRequest( new ProblemDetails { Detail = $"There's no event with id {model.EventId}." })); } var entity = new DataAccess.Models.Ticket(); _mapper.Map(model, entity); entity.TicketGuid = Guid.NewGuid(); entity.TicketNumber = entity.TicketNumber ?? TicketNumberHelper.GenerateTicketNumber(evt); SetAuthorInfo(entity); _context.Add(entity); _context.SaveChanges(); model = _mapper.Map <Ticket>(entity); return(CreatedAtAction(nameof(GetById), new { id = model.Id }, model)); }
public void CreateEvent(Event model) { var sportEvent = new Event { EventStartDate = model.EventStartDate }; db.Events.Add(sportEvent); db.SaveChanges(); }
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 <Client> CreateClient([FromBody] Client model) { if (model.Id != Guid.Empty) { return(BadRequest()); } var entity = _mapper.Map <ApplicationCore.Models.Client>(model); entity.CreatedAt = DateTime.UtcNow; _context.Add(entity); _context.SaveChanges(); model = _mapper.Map <Client>(entity); return(CreatedAtAction(nameof(GetClientByIdAsync), new { id = model.Id }, model)); }
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 ActionResult <Event> CreateEvent([FromBody] Event model) { if (model.Id > 0) { return(BadRequest()); } var entity = new DataAccess.Models.Event(); _mapper.Map(model, entity); _context.Add(entity); _context.SaveChanges(); model = _mapper.Map <Event>(entity); return(CreatedAtAction(nameof(GetEvent), new { id = model.Id }, model)); }
public IActionResult UpdateArtistEvent([FromRoute] int idArtist, [FromRoute] int idEvent, [FromBody] ArtistEvent request) { var artistEvent = _context.ArtistEvent .Where(ae => ae.IdArtist == idArtist) .Where(ae => ae.IdEvent == idEvent) .Include(ae => ae.Event) .FirstOrDefault(); if (artistEvent == null) { return(NotFound()); } if (artistEvent.Event.StartDate > DateTime.Now) { return(BadRequest($"event (id: {idEvent}) already started ")); } if (request.PerformanceDate > artistEvent.Event.EndDate || request.PerformanceDate < artistEvent.Event.StartDate) { // TODO detailed error msg return(BadRequest("new performanceDate must be set within event startTime and endTime limits")); } artistEvent.PerformanceDate = request.PerformanceDate; _context.Update(artistEvent); _context.SaveChanges(); return(Ok(artistEvent)); }
public async Task <ActionResult> UpdateMailSettingsAsync(Guid eventId, MailSettings values) { var evt = await _context.Events .Include(e => e.MailSettings) .ThenInclude(x => x.DemoEmailRecipients) .FirstOrDefaultAsync(e => e.Id == eventId); if (evt == null) { return(BadRequest( new ProblemDetails { Detail = $"There's no event with id {eventId}." })); } if (evt.MailSettings == null) { evt.MailSettings = new ApplicationCore.Models.MailSettings(); } _mapper.Map(values, evt.MailSettings); evt.MailSettings.DemoEmailRecipients.Clear(); if (values.DemoEmailRecipients?.Any() == true) { evt.MailSettings.DemoEmailRecipients = values.DemoEmailRecipients .Select(e => new ApplicationCore.Models.DemoEmailRecipient(e)) .ToList(); } _context.SaveChanges(); return(NoContent()); }
public IActionResult ValidateTicket(string id) { Guid ticketGuid; if (!Guid.TryParse(id, out ticketGuid)) { _logger.LogInformation("The parameter id is no valid guid."); return(TicketNotFound()); } DataAccess.Models.Ticket ticket = _context.Tickets .Include(e => e.Event) .Include(e => e.TicketType) .SingleOrDefault(e => e.TicketGuid == ticketGuid); if (ticket == null) { _logger.LogInformation("Ticket with id {id} was not found in the database.", ticketGuid); return(TicketNotFound()); } if (ticket.Validated) { _logger.LogInformation("The ticket has been already used before."); return(View("TicketUsed", ticket)); } ticket.Validated = true; _context.SaveChanges(); return(View("TicketValid", ticket)); }
private async Task <IActionResult> ValidateTicketAsync(ApplicationCore.Models.Ticket ticket, string ticketNumber = null, string ticketSecret = null) { if (ticket == null) { string lookupValueType = ticketNumber == null ? "secret" : "number"; string lookupValue = ticketNumber ?? ticketSecret; _logger.LogInformation( $"Ticket with {lookupValueType} {lookupValue} was not found in the database.", lookupValue); return(TicketNotFound()); } ClaimsPrincipal currentUser = await TryGetAuthenticatedUser(); UserContext context = currentUser.GetContext(); // If the user is logged in for a single event only, // check if the ticket and the event are the same. if (context.EventId != null && context.EventId != ticket.EventId) { return(WrongEvent()); } if (!currentUser.Identity.IsAuthenticated) { _logger.LogInformation("Unauthorized. Redirect to event homepage."); string validationUri = GetTicketValidationUri(ticket.TicketSecret); string redirectUrl = await _ticketRedirectService.GetRedirectUrlAsync(ticket.Id, validationUri); return(Redirect(redirectUrl)); } var currentEvent = ticket.Event; if (currentEvent == null) { // try to get the event for which the master qr code was issued. UserContext userContext = User.GetContext(); if (userContext?.EventId != null) { currentEvent = _context.Events.Find(userContext.EventId); } } if (currentEvent != null && currentEvent.IsConference) { return(ConferenceCheckInDialog(ticket)); } if (ticket.Validated) { _logger.LogInformation("The ticket has already been used before."); return(View("TicketUsed", ticket)); } ticket.Validated = true; _context.SaveChanges(); return(View("TicketValid", ticket)); }
public EventRepositoryTest() { _dbOptions = new DbContextOptionsBuilder <EventsDbContext>() .UseInMemoryDatabase(databaseName: "in-memory") .Options; _dbContext = new EventsDbContext(_dbOptions); _dbContext.AddRange(GetFaceEvents()); _dbContext.SaveChanges(); this._repository = new EventRepository(_dbContext); }
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 void AddRange(IEnumerable <TEntity> entityList, bool saveChanges = true) { Querable.AddRange(entityList); if (saveChanges) { DBContext.SaveChanges(); } }
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); }
public ActionResult ExampleForm(Event @event) { //event is a reserved keyword, so we have to prefix an '@' to use it using (var dbContext = new EventsDbContext()) { //add to the in memory collection dbContext.Events.Add(@event); //persist these data changes to the db dbContext.SaveChanges(); //on successful saving of data, go to a //different ACTION METHOD to start a new process return(RedirectToAction("ExampleList")); } }
// GET: GuestBookings/UpdateAttendance/Customer/5/Event/5 public async Task <IActionResult> UpdateAttendance(int?customerId, int?eventId) { if (customerId == null || eventId == null || !GuestBookingExists(customerId, eventId)) { return(NotFound()); } var current = await _context.Guests .Include(g => g.Customer) .Include(g => g.Event) .FirstOrDefaultAsync(m => m.CustomerId == customerId && m.EventId == eventId); if (current == null) { return(NotFound()); } current.Attended = !current.Attended; _context.SaveChanges(); return(RedirectToAction("Index", new { id = eventId })); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new User { UserName = model.UserName, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Address = model.Address, PhoneNumber = model.PhoneNumber }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); var player = new Player { UserName = user.UserName, UserId = user.Id }; var eventsContext = new EventsDbContext(); eventsContext.Players.Add(player); eventsContext.SaveChanges(); return RedirectToAction("Index", "Home"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }
public int Commit() { return(_context.SaveChanges()); }
public void Save() { _context.SaveChanges(); }
public IEntity Add(IEntity entity) { _context.Entry(entity).State = EntityState.Added; _context.SaveChanges(); return(entity); }