protected void RefreshContext() { var refreshableObjects = Context.ChangeTracker.Entries().Select(c => c.Entity).ToList(); foreach (var item in refreshableObjects) { Context.Entry(item).Reload(); } }
public async Task <IActionResult> UpdateMember(int id, Member member) { member.Id = id; if (member.Organized) { member.Role = UserRole.Organizer; } else { member.Role = UserRole.Member; } _context.Entry(member).State = EntityState.Modified; await _context.SaveChangesAsync(); return(Ok(member)); }
public async Task <IActionResult> Handle(UpdateGroupCodeCommand request, CancellationToken cancellationToken) { var groupCode = await _context.GroupCodes.FirstOrDefaultAsync(x => x.Id == request.GroupCodeId); if (groupCode == null) { return(HttpResponseCodeHelper.NotFound("El Grupo de codigo no existe")); } _mapper.Map(request, groupCode); _context.Entry(groupCode).State = EntityState.Modified; await _context.SaveChangesAsync(); return(HttpResponseCodeHelper.NotContent()); }
public async Task <IActionResult> Handle(UpdateEventCommand request, CancellationToken cancellationToken) { var eventToUpdate = await _context.Events.Include(x => x.Sponsors).FirstOrDefaultAsync(x => x.Id == request.Id); if (eventToUpdate == null) { return(new StatusCodeResult(402)); } _mapper.Map(request, eventToUpdate); if (request?.Live == true && !eventToUpdate.Live) { eventToUpdate.SetLive(); } else if (request?.Live == false) { eventToUpdate.SetUnLive(); } if (request?.GeneralAttended == true && !eventToUpdate.GeneralAttended) { eventToUpdate.EnableGeneralAttendace(); } else if (request?.GeneralAttended == false) { eventToUpdate.DisableGeneralAttendace(); } _context.Entry(eventToUpdate).State = EntityState.Modified; await _context.SaveChangesAsync(); return(new ObjectResult(_mapper.Map(eventToUpdate, new EventDetail())) { StatusCode = 200 }); }
public async Task <AuthenticateUser> AuthenticateOrCreate(string email, int meetupId) { var userByEmail = _context.Members.Include(x => x.Events).SingleOrDefault(x => x.Email.ToUpper() == email.ToUpper()); var userByMeetupId = _context.Members.SingleOrDefault(x => x.MeetupId == meetupId && x.Email.ToUpper() != email.ToUpper()); var id = 0; UserRole rol = UserRole.Member; // return null if user not found if (userByEmail == null && userByMeetupId == null) { userByEmail = new Member { Email = email, Role = UserRole.Member, MeetupId = meetupId }; await _context.Members.AddAsync(userByEmail); await _context.SaveChangesAsync(); id = userByEmail.Id; rol = UserRole.Member; } else { if (userByEmail == null && userByMeetupId != null) { userByMeetupId.Email = email.ToUpper(); id = userByMeetupId.Id; rol = userByMeetupId.Role; } else if (userByEmail != null && userByMeetupId == null) { userByEmail.MeetupId = meetupId; id = userByEmail.Id; rol = userByEmail.Role; } else if (userByEmail != null && userByMeetupId != null) { userByEmail.MeetupId = meetupId; var attendances = await _context.Attendances.Where(x => x.MemberId == userByMeetupId.Id).ToListAsync(); _context.Entry(userByMeetupId).State = EntityState.Deleted; await _context.SaveChangesAsync(); foreach (var attendance in attendances) { var newToInsert = mapper.Map(attendance, new Attendance()); newToInsert.Member = userByEmail; newToInsert.MemberId = userByEmail.Id; _context.Entry(newToInsert).State = EntityState.Added; await _context.Attendances.AddAsync(newToInsert); } id = userByEmail.Id; rol = userByEmail.Role; await _context.SaveChangesAsync(); } } return(new AuthenticateUser(TokenService.Generate(_appSettings.Secret, new List <CustomClaim> { new CustomClaim(ClaimTypes.Name, id.ToString()), new CustomClaim(ClaimTypes.Email, email), new CustomClaim(ClaimTypes.Role, rol.ToString()) }, DateTime.UtcNow.AddDays(30)))); }