예제 #1
0
        public async Task <UpdateDancerPayload> UpdateDancerAsync(
            UpdateDancerInput input,
            [ScopedService] DatabaseContext context,
            [Service] IAuthorization authorization,
            [Service] IFileStorage fileStorage,
            CancellationToken cancellationToken)
        {
            var authId = authorization.GetUserId();
            var dancer = await context.Dancers.FindAsync(new object[] { input.DancerId }, cancellationToken);

            if (dancer.AuthenticationId != authId)
            {
                return(new UpdateDancerPayload(
                           new []
                {
                    new UserError("Cannot update unmatched subject.", CommonErrorCodes.ACT_AGAINST_INVALID_SUBJECT)
                }));
            }

            if (dancer is null)
            {
                return(new UpdateDancerPayload(
                           new []
                {
                    new UserError("Dancer not found.", DancerErrorCodes.DANCER_NOT_FOUND)
                }));
            }

            dancer.DdrCode = input.DdrCode;
            dancer.DdrName = input.DdrName;
            dancer.State   = input.State;
            dancer.PrimaryMachineLocation = input.PrimaryMachineLocation;

            if (input.ProfilePicture != null)
            {
                dancer.ProfilePictureTimestamp = DateTime.UtcNow;
                try
                {
                    using var profileImage = await Image.LoadAsync(input.ProfilePicture.OpenReadStream());

                    var image = await Images.ImageToPngMemoryStreamFactor(profileImage, 256, 256);

                    var destinationKey = $"profile/picture/{dancer.Id}.{(dancer.ProfilePictureTimestamp?.Ticks - 621355968000000000) / 10000000}.png";
                    await fileStorage.UploadFileFromStream(image, destinationKey);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return(new UpdateDancerPayload(
                               new []
                    {
                        new UserError("Failed to upload profile picture.", CommonErrorCodes.IMAGE_UPLOAD_FAILED)
                    }));
                }
            }

            await context.SaveChangesAsync(cancellationToken);

            return(new UpdateDancerPayload(dancer));
        }
예제 #2
0
        public async Task <UpdateDancerPayload> UpdateDancerAsync(
            UpdateDancerInput input,
            [ScopedService] DatabaseContext context,
            [Service] IAuthorization authorization,
            CancellationToken cancellationToken)
        {
            var authId = authorization.GetUserId();
            var dancer = await context.Dancers.FindAsync(new object[] { input.DancerId }, cancellationToken);

            if (dancer.AuthenticationId != authId)
            {
                return(new UpdateDancerPayload(
                           new []
                {
                    new UserError("Cannot update unmatched subject.", CommonErrorCodes.ACT_AGAINST_INVALID_SUBJECT)
                }));
            }

            if (dancer is null)
            {
                return(new UpdateDancerPayload(
                           new []
                {
                    new UserError("Dancer not found.", DancerErrorCodes.DANCER_NOT_FOUND)
                }));
            }

            dancer.DdrCode = input.DdrCode;
            dancer.DdrName = input.DdrName;
            dancer.State   = input.State;
            dancer.PrimaryMachineLocation = input.PrimaryMachineLocation;

            await context.SaveChangesAsync(cancellationToken);

            return(new UpdateDancerPayload(dancer));
        }