Esempio n. 1
0
        public async Task <IActionResult> CreateInvite([FromBody] CreateInviteCommand command)
        {
            var result = await _mediator.Send(command);

            var invite = _mapper.Map <GetInviteQuery>(result);

            return(CreatedAtRoute("GetInvite", new { inviteid = result.Id }, invite));
        }
        public async Task <Result <InviteLinkDto> > Handle(CreateInviteCommand request, CancellationToken cancellationToken)
        {
            // Get the classroom.
            var classroom = await _context.Classrooms.FindAsync(request.ClassroomId);

            // No classroom  was found with the given id.
            if (classroom is null)
            {
                return(Result <InviteLinkDto> .Failure("Unable to find classroom."));
            }

            // Get the currently authorized user.
            var user = await _context.Users.SingleOrDefaultAsync(x =>
                                                                 x.UserName == _userAccessor.GetCurrentUsername());

            // Create a new entry in the InviteLink table.
            var link = new InviteLink {
                Id                  = request.Id,
                Token               = new ShortGuid(Guid.NewGuid()),
                ExpiryDate          = DateTime.Now.AddDays(1),
                ExpireAfterFirstUse = false,
                Hits                = 0
            };

            _context.InviteLinks.Add(link);

            var linkToReturn = _mapper.Map <InviteLink, InviteLinkDto>(link);

            // Save changes to the database.
            var success = await _context.SaveChangesAsync() > 0;

            if (success)
            {
                return(Result <InviteLinkDto> .Success(linkToReturn));
            }

            return(Result <InviteLinkDto> .Failure("There was a problem saving changes."));
        }
Esempio n. 3
0
 public async Task <IActionResult> Create(CreateInviteCommand command)
 {
     return(HandleResult(await Mediator.Send(command)));
 }