Beispiel #1
0
        public async Task <ActionResult> CreateAsync(
            CreateOrUpdateSpaceRequest request)
        {
            var space = await this._spaceInteractionService
                        .CreateSpaceAsync(request);

            if (space.Left != null)
            {
                return(this.Ok(space.Left));
            }
            else
            {
                return(this.BadRequest(space.Right !));
            }
        }
Beispiel #2
0
        public ActionResult Update(
            string spaceId,
            CreateOrUpdateSpaceRequest request)
        {
            if (spaceId != request.SpaceId)
            {
                return(this.BadRequest(
                           new ValidationError("spaceId", "INVALID_SPACE_ID")));
            }

            var space = this._spaceInteractionService.UpdateSpace(request);

            if (space.Left == null && space.Right == null)
            {
                return(this.NotFound());
            }
            else if (space.Left != null)
            {
                return(this.Ok(space.Left));
            }

            return(this.BadRequest(space.Right));
        }
        CreateSpaceAsync(CreateOrUpdateSpaceRequest request)
        {
            if (!await this._authorizationService
                .IsAuthorizedAsync("create_space"))
            {
                return(new Models.Either <SpaceResponse, ValidationError>(
                           new ValidationError("authorization", "INVALID_SCOPE")));
            }

            try
            {
                if (!string.IsNullOrEmpty(request.VanityUrl))
                {
                    var uri = new Uri(request.VanityUrl);
                    if (
                        uri.Host != "howler.gg" ||
                        uri.Scheme != "https" ||
                        !uri.IsDefaultPort)
                    {
                        return(new Models
                               .Either <SpaceResponse, ValidationError>(
                                   new ValidationError("vanityUrl", "INVALID_URL")));
                    }

                    var vanity = this._indexerDatabaseContext.SpaceVanityUrls
                                 .Where(
                        v => v.VanityUrl == request.VanityUrl.Trim()
                        .ToLowerInvariant()).ToList().FirstOrDefault();

                    if (vanity != null)
                    {
                        return(new Models
                               .Either <SpaceResponse, ValidationError>(
                                   new ValidationError("vanityUrl", "ALREADY_TAKEN")));
                    }
                }
            }
            catch (UriFormatException)
            {
                return(new Models.Either <SpaceResponse, ValidationError>(
                           new ValidationError("vanityUrl", "INVALID_URL")));
            }

            if (Guid.TryParse(request.SpaceId, out Guid spaceId) &&
                !this._coreDatabaseContext.Spaces.Where(
                    s => s.SpaceId == request.SpaceId.ToLowerInvariant())
                .ToList().Any())
            {
                var space = new Space
                {
                    SpaceId          = request.SpaceId,
                    SpaceName        = request.SpaceName,
                    Description      = request.Description,
                    VanityUrl        = request.VanityUrl?.Trim().ToLowerInvariant(),
                    ServerUrl        = "https://api.howler.chat",
                    CreatedDate      = DateTime.UtcNow,
                    ModifiedDate     = DateTime.UtcNow,
                    DefaultChannelId = request.SpaceId,
                    UserId           = this._authorizationService.User !.Subject,
                    ChannelGroups    = Encoding.UTF8.GetBytes(
                        JsonConvert.SerializeObject(
                            new ChannelGroupResponse
                    {
                        { "General", new string[] { request.SpaceId } },
                    })),
                };

                this._coreDatabaseContext.Spaces.Add <Space, string>(space);

                if (request.VanityUrl != null)
                {
                    var vanity = new SpaceVanityUrl
                    {
                        VanityUrl = request.VanityUrl?.Trim()
                                    .ToLowerInvariant(),
                        SpaceId = request.SpaceId,
                    };
                    this._indexerDatabaseContext.SpaceVanityUrls
                    .Add <SpaceVanityUrl, string>(vanity);
                }

                this._coreDatabaseContext.Channels
                .Add <Channel, Tuple <string, string> >(new Channel
                {
                    ChannelId    = request.SpaceId,
                    SpaceId      = request.SpaceId,
                    ChannelName  = "general",
                    MemberId     = this._authorizationService.User !.Subject,
                    CreatedDate  = DateTime.UtcNow,
                    ModifiedDate = DateTime.UtcNow,
                });