public async Task <IActionResult> CreateTemplate([FromBody] CaseTemplateForCreateDto templateDto, [FromQuery] ulong guildId)
        {
            Identity currentIdentity = await GetIdentity();

            if (!await currentIdentity.HasPermissionOnGuild(DiscordPermission.Moderator, guildId))
            {
                throw new UnauthorizedException();
            }


            CaseTemplate template = new()
            {
                TemplateName           = templateDto.TemplateName,
                UserId                 = currentIdentity.GetCurrentUser().Id,
                ViewPermission         = templateDto.ViewPermission,
                CreatedForGuildId      = guildId,
                CreatedAt              = DateTime.UtcNow,
                CaseTitle              = templateDto.Title,
                CaseDescription        = templateDto.Description,
                CaseLabels             = templateDto.Labels,
                CasePunishedUntil      = templateDto.PunishedUntil,
                CasePunishmentType     = templateDto.PunishmentType,
                SendPublicNotification = templateDto.SendPublicNotification,
                AnnounceDm             = templateDto.AnnounceDm,
                HandlePunishment       = templateDto.HandlePunishment
            };

            CaseTemplate createdTemplate = await CaseTemplateRepository.CreateDefault(_serviceProvider, currentIdentity).CreateTemplate(template);

            return(StatusCode(201, new CaseTemplateView(createdTemplate)));
        }
        public async Task <IActionResult> DeleteTemplate([FromRoute] int templateId)
        {
            Identity currentIdentity = await GetIdentity();

            CaseTemplateRepository repo = CaseTemplateRepository.CreateDefault(_serviceProvider, currentIdentity);

            CaseTemplate template = await repo.GetTemplate(templateId);

            if (!await currentIdentity.IsAllowedTo(APIActionPermission.Delete, template))
            {
                return(Unauthorized());
            }

            await repo.DeleteTemplate(template);

            return(Ok(new CaseTemplateView(template)));
        }
예제 #3
0
        public async Task <IActionResult> GetTemplatesView([FromQuery] ulong userId = 0)
        {
            Identity currentIdentity = await GetIdentity();

            CaseTemplateRepository repository = CaseTemplateRepository.CreateDefault(_serviceProvider, currentIdentity);
            List <CaseTemplate>    templates  = await repository.GetTemplatesBasedOnPermissions();

            List <CaseTemplateExpandedView> templatesView = new();

            foreach (var template in templates.Where(x => x.UserId == userId || userId == 0))
            {
                templatesView.Add(new CaseTemplateExpandedView(
                                      template,
                                      await _discordAPI.FetchUserInfo(template.UserId, CacheBehavior.OnlyCache),
                                      _discordAPI.FetchGuildInfo(template.CreatedForGuildId, CacheBehavior.Default)
                                      ));
            }

            return(Ok(templatesView));
        }