public async Task <string> AddNewInvitation(InvitationVM invitationVM, int id) { var senderId = userService.GetUserId(); var sender = await context.Users.FirstOrDefaultAsync(x => x.Id == senderId); var receiver = await context.Users.FirstOrDefaultAsync(x => x.Email == invitationVM.Email); var group = await context.Groups.FirstOrDefaultAsync(x => x.GroupId == id); var assignmentTmp = await context.Assignments .FirstOrDefaultAsync(x => x.User == receiver && x.GroupId == id); var invitationTmp = await context.Invitations .FirstOrDefaultAsync(x => x.GroupId == id && x.Receiver == receiver && x.Status == "Oczekujące"); var role = await context.Roles.FirstOrDefaultAsync(x => x.Name == invitationVM.Role); if (sender == receiver) { return("Nie możesz wysłać zaproszenia do samego siebie."); } if (receiver == null) { return("Nie znaleziono użytkownika z podanem adresem E-mail."); } if (assignmentTmp != null) { return("Zapraszany użytkownik już należy do tej grupy."); } if (invitationTmp != null) { return("Istnieje już takie samo zaproszenie"); } var newInvitation = new Invitation() { Sender = sender, Receiver = receiver, Status = "Oczekujące", Group = group, CreatedAt = DateTime.Now, Role = role, RoleId = role.RoleId }; await context.Invitations.AddAsync(newInvitation); await context.SaveChangesAsync(); return("Sukces"); }
public async Task <IActionResult> AddNewInvitation(InvitationVM invitationVM, int groupId) { if (ModelState.IsValid) { string result = await invitationRepository.AddNewInvitation(invitationVM, groupId); if (result == "Sukces") { return(RedirectToAction(nameof(AddNewInvitation), new { isSuccess = true, groupId = groupId })); } else { ModelState.AddModelError("", result); } } return(View()); }