Пример #1
0
        public async Task <CommandResult> AddPhotoToObject(int objectId, IFormFile image)
        {
            var authorizationResult = _authorizer.IsAuthorized(o => o.OfferedObjectId == objectId, (o) => o.OwnerLogin.User);

            if (!authorizationResult)
            {
                return(new CommandResult(new ErrorMessage
                {
                    ErrorCode = "OBJECT.PHOTO.UNAUTHORIZED",
                    Message = "You are not authorized to add a photo to this object",
                    StatusCode = System.Net.HttpStatusCode.Unauthorized
                }));
            }


            var @object = _objectsRepo.Get(objectId);

            if (@object is null || @object.ObjectStatus != ObjectStatus.Available)
            {
                return(new CommandResult(new ErrorMessage
                {
                    ErrorCode = "OBJECT.DOES.NOT.EXISTS",
                    Message = "You are not authorized to add a photo to this object",
                    StatusCode = System.Net.HttpStatusCode.Unauthorized
                }));
            }

            var savingResult = await _imageSaver.SaveImageAsync(image);

            if (!savingResult.IsSuccessful)
            {
                return(new CommandResult(new ErrorMessage
                {
                    ErrorCode = savingResult.Error.ErrorCode,
                    Message = savingResult.Error.Message,
                    StatusCode = savingResult.Error.StatusCode,
                }));
            }

            var newPhoto = new ObjectPhoto
            {
                AddedAtUtc            = DateTime.UtcNow,
                ObjectId              = objectId,
                AdditionalInformation = QueryString.Create(new Dictionary <string, string>
                {
                    { "Name", savingResult.Result.Name.ToString() },
                    { "Version", "1" }
                }).ToUriComponent(),
                FilePath = savingResult.Result.Path,
            };

            _photoRepo.Add(newPhoto);
            await _photoRepo.SaveChangesAsync();

            return(new CommandResult());
        }
Пример #2
0
 public string Construct(ObjectPhoto objectPhoto)
 {
     return($"{_currentDomain}/Resources/Photo/Object/{HttpUtility.ParseQueryString(objectPhoto.AdditionalInformation)["Name"]}");
 }