Example #1
0
        public async Task <IActionResult> OnPostAsync(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Club = await _context.Clubs
                   .Where(c => c.Id == id)
                   .Include(c => c.ClubMembers)
                   .FirstOrDefaultAsync();

            if (Club == null)
            {
                return(NotFound());
            }

            var founderId = await _context.ClubMembers
                            .Where(cm => cm.ClubId == Club.Id && cm.Role == EClubMemberRoles.Founder)
                            .Select(cm => cm.MemberId)
                            .FirstOrDefaultAsync();

            if (User.IsUserSameAsLoggedIn(founderId))
            {
                return(Unauthorized());
            }

            _context.Clubs.Remove(Club);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
Example #2
0
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            var uid = User.GetNumericId();

            if (uid is null)
            {
                return(Unauthorized());
            }

            var club = new Data.Models.Club
            {
                Name        = Input.Name,
                Slug        = Input.Name.Friendlify(),
                Hook        = Input.Hook,
                Description = Input.Description,
            };
            await _context.Clubs.AddAsync(club);

            var member = new ClubMember
            {
                MemberId = (long)uid,
                Role     = EClubMemberRoles.Founder
            };

            club.ClubMembers.Add(member);

            await _context.SaveChangesAsync();

            if (Input.Icon != null && Input.Icon.Length > 0)
            {
                var file = await _uploader.Upload(
                    Input.Icon,
                    "club-icons",
                    $"{club.Id}-{club.Name.Friendlify()}",
                    _config.ClubIconWidth,
                    _config.ClubIconHeight
                    );

                club.IconId = file.FileId;
                club.Icon   = file.Path;
                // Final save
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Example #3
0
        public async Task <IActionResult> OnGetAsync(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var club = await _context.Clubs
                       .Where(c => c.Id == id)
                       .Select(c => new
            {
                Club      = c,
                FounderId = c.ClubMembers.First(cm => cm.Role == EClubMemberRoles.Founder).MemberId
            })
                       .AsNoTracking()
                       .FirstOrDefaultAsync();

            if (club == null)
            {
                return(NotFound());
            }

            Club = club.Club;

            if (Club == null)
            {
                return(NotFound());
            }

            if (!User.IsUserSameAsLoggedIn(club.FounderId))
            {
                return(Unauthorized());
            }

            return(Page());
        }