public async Task InviteUser(PlanInvitation newInvitation) { try { _dbContext.PlanInvitations.Add(newInvitation); var result = await _dbContext.SaveChangesAsync(); if (result <= 0) { throw new Exception("Could not add invitation in db"); } } catch (DbUpdateException exc) { if (exc.InnerException is SqlException sqlExc) { switch (sqlExc.Number) { //2627 is unique id already exists case 2627: throw new UniqueConstraintException("Invitation has already been sent"); default: throw; } } } catch (Exception exc) { throw; } }
public async Task InviteUser(Guid inviter, string inviteeUsername, Guid travelPlanId) { try { var travelPlan = await _travelPlanRepository.GetAsync(travelPlanId, includeUTP : true); //validate the inviter is the host if (travelPlan.CreatedById != inviter) { //log here throw new InsufficientRightsException("User doesn't have rights to add to travelplan"); } //validate invitee exists var userToInvite = await _userRepository.GetUserAsync(inviteeUsername); if (userToInvite == null) { //log here throw new UserNotFoundException("User to add does not exist"); } //check if user to invite is already part of plan if (travelPlan.UserTravelPlans.Exists((utp) => utp.TravelPlanId == new Guid(userToInvite.Id))) { throw new CommonException("User is already a traveler!"); } var newInvitation = new PlanInvitation { Created = DateTime.UtcNow, Expiration = DateTime.UtcNow.AddDays(7), InvitedById = inviter, InviteeId = new Guid(userToInvite.Id), TravelPlanId = travelPlanId }; await _planInvitationRepository.InviteUser(newInvitation); } catch (Exception) { throw; } }
public async Task DeleteInvitation(PlanInvitation invitation) { try { //accepting means remove the invitation from table _dbContext.PlanInvitations.Remove(invitation); var result = await _dbContext.SaveChangesAsync(); if (result <= 0) { //log here throw new Exception("Could not save invitation changes"); } } catch (Exception) { throw; } }
public async Task InviteUser(Guid inviter, string inviteeUsername, Guid travelPlanId) { try { //get travel plan var travelPlan = await _dbContext.TravelPlans.Where(tp => tp.TravelPlanId == travelPlanId) .Include((tp) => tp.UserTravelPlans) .FirstOrDefaultAsync(); //validate the inviter is the host if (travelPlan.CreatedById != inviter) { //log here throw new InsufficientRightsException("User doesn't have rights to add to travelplan"); } //validate invitee exists var userToInvite = await _userRepository.GetUserAsync(inviteeUsername); if (userToInvite == null) { //log here throw new UserNotFoundException("User to add does not exist"); } //check if user to invite is already part of plan if (travelPlan.UserTravelPlans.Exists((utp) => utp.UserId == new Guid(userToInvite.Id))) { throw new CommonException("User is already a traveler!"); } var newInvitation = new PlanInvitation { Created = DateTime.UtcNow, Expiration = DateTime.UtcNow.AddDays(7), InvitedById = inviter, InviteeId = new Guid(userToInvite.Id), TravelPlanId = travelPlanId }; _dbContext.PlanInvitations.Add(newInvitation); var result = await _dbContext.SaveChangesAsync(); if (result <= 0) { throw new Exception("Could not add invitation in db"); } } catch (DbUpdateException exc) { if (exc.InnerException is SqlException sqlExc) { switch (sqlExc.Number) { case 2627: throw new UniqueConstraintException("Invitation has already been sent"); default: throw; } } } catch (Exception exc) { throw; } }