public async Task <ActionResult> AddUserToEvent(int eventId, AddUserToEventDTO addUserToEventDTO) { var userId = HttpContext.User.Claims.FirstOrDefault(y => y.Type == ClaimTypes.NameIdentifier); if (userId == null || userId.Value != addUserToEventDTO.UserId) { return(Forbid()); } var userExist = await context.Users.AnyAsync(u => u.Id == addUserToEventDTO.UserId); if (!userExist) { return(NotFound()); } var actualEvent = await context.Events.FirstOrDefaultAsync(e => e.Id == eventId); if (actualEvent == null) { return(NotFound()); } var userInscription = new ApplicationUserEvents { EventId = eventId, UserId = addUserToEventDTO.UserId, InscriptionDate = DateTime.Now, IsActive = true }; context.Add(userInscription); var eventActivity = new UserActivity { Text = $"Te has unido al evento {actualEvent.Name}", UserId = addUserToEventDTO.UserId }; context.Add(eventActivity); bool haveBadge = await context.ApplicationUserBadges.AnyAsync(b => b.UserId == addUserToEventDTO.UserId && b.BadgeId == 2); if (!haveBadge) { var userBadge = new ApplicationUserBadges { BadgeId = 2, UserId = addUserToEventDTO.UserId }; context.Add(userBadge); } await context.SaveChangesAsync(); return(NoContent()); }
public async Task <ActionResult> UpdateUserProfile(string userId, [FromForm] UpdateUserProfileDTO updateUserProfileDTO) { //TODO //Chequear el role de administrador o que el usuario este editando su propio perfil var userIdC = HttpContext.User.Claims.FirstOrDefault(y => y.Type == ClaimTypes.NameIdentifier); if (userIdC == null || userIdC.Value != userId) { return(Forbid()); } var user = await applicationDbContext.Users.FirstOrDefaultAsync(u => u.Id == userId); if (user == null) { return(NotFound()); } mapper.Map(updateUserProfileDTO, user); if (updateUserProfileDTO.ProfilePicture != null) { var bytes = await updateUserProfileDTO.ProfilePicture.ConvertToByteArray(); if (!string.IsNullOrEmpty(user.ProfilePicture)) { await fileStorage.RemoveFile(user.ProfilePicture, ApplicationConstants.ImageContainerNames.ProfilePicturesContainer); } user.ProfilePicture = await fileStorage.SaveFile(bytes, Path.GetExtension(updateUserProfileDTO.ProfilePicture.FileName), ApplicationConstants.ImageContainerNames.ProfilePicturesContainer, updateUserProfileDTO.ProfilePicture.ContentType, Guid.NewGuid().ToString()); } applicationDbContext.Entry(user).State = EntityState.Modified; bool haveSociableBadge = await applicationDbContext.ApplicationUserBadges.AnyAsync(x => x.UserId == userId && x.BadgeId == 1); if (!haveSociableBadge) { var sociableBadge = new ApplicationUserBadges { UserId = userId, BadgeId = 1, GetDate = DateTime.UtcNow }; applicationDbContext.Add(sociableBadge); } await applicationDbContext.SaveChangesAsync(); return(NoContent()); }
public async Task <ActionResult <GroupJoinResult> > AddGroupMember(int groupId, AddGroupMemberDTO addGroupMemberDTO) { var userId = HttpContext.User.Claims.FirstOrDefault(y => y.Type == ClaimTypes.NameIdentifier); if (userId == null || userId.Value != addGroupMemberDTO.UserId) { return(Forbid()); } var user = await applicationDbContext.Users.FirstOrDefaultAsync(u => u.Id == addGroupMemberDTO.UserId); if (user == null) { return(NotFound()); } if (user.GroupId.HasValue) { return(new GroupJoinResult { Success = false, AlreadyInAgroup = true, AlreadyInThisGroup = user.GroupId == groupId }); } var group = await applicationDbContext.Groups .Include(x => x.Users).FirstOrDefaultAsync(g => g.Id == groupId); if (group == null) { return(NotFound()); } bool isLeader = !group.Users.Any(u => u.IsGroupLeader); applicationDbContext.Attach(user); user.IsGroupLeader = isLeader; user.GroupId = groupId; var groupActivity = new UserActivity { Text = $"Te has unido al grupo {group.Name}", UserId = addGroupMemberDTO.UserId }; applicationDbContext.Add(groupActivity); bool haveBadge = await applicationDbContext.ApplicationUserBadges.AnyAsync(b => b.UserId == user.Id && b.BadgeId == 3); if (!haveBadge) { var userBadge = new ApplicationUserBadges { BadgeId = 3, UserId = user.Id, GetDate = DateTime.UtcNow }; applicationDbContext.Add(userBadge); } await applicationDbContext.SaveChangesAsync(); return(new GroupJoinResult { Success = true }); }