コード例 #1
0
        public async Task <IActionResult> Invite([Bind] InviteFormViewModel values)
        {
            var user = await GetCurrentUserAsync();

            if (ModelState.IsValid)
            {
                if (values.DaysValidFor <= 0)
                {
                    ModelState.AddModelError(nameof(values.DaysValidFor), "Invalid number of days");
                }
                else if (string.IsNullOrWhiteSpace(values.Message))
                {
                    ModelState.AddModelError(nameof(values.Message), "Message is required");
                }
                else if (values.Type == InvitationType.Register && !User.IsInRole("admin"))
                {
                    ModelState.AddModelError(string.Empty, "Must be an admin to create group invitations");
                }
                else
                {
                    var code = await _invitationService.NewInvitation(user.UserId.Value, values.Type, values.UserIdTo, values.Message, values.DaysValidFor);

                    if (code != null)
                    {
                        return(RedirectToAction(nameof(Invites), new { selected = code.Code }));
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Error creating invitation");
                    }
                }
            }
            return(View(new InviteViewModel()
            {
                Local = user.PrivateModel as UserProfileModel,
                MyProfile = user,
                Values = values
            }));
        }