Example #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());
        }
Example #2
0
        public async Task <CommandResult <Tag> > AddTag(AddTagDto tag)
        {
            if (tag is null)
            {
                return(new ErrorMessage
                {
                    ErrorCode = "CATALOG.TAG.ADD.NULL",
                    Message = "Please send a valid data",
                    StatusCode = System.Net.HttpStatusCode.BadRequest
                }.ToCommand <Tag>());
            }

            if (tag.TagName.IsNullOrEmpty())
            {
                return(new ErrorMessage
                {
                    ErrorCode = "CATALOG.TAG.ADD.NAME.EMPTY",
                    Message = "Please send a valid Tag name",
                    StatusCode = System.Net.HttpStatusCode.BadRequest
                }.ToCommand <Tag>());
            }


            var existedTags = from t in _tagsRepo.Table
                              where t.Name == tag.TagName && t.TagStatus == TagStatus.Ok
                              select t;

            if (existedTags.Any())
            {
                return(new ErrorMessage
                {
                    ErrorCode = "CATALOG.TAG.ADD.ALREADY.EXISTED",
                    Message = "The tag you are trying to add is already existed.",
                    StatusCode = System.Net.HttpStatusCode.BadRequest
                }.ToCommand <Tag>());
            }


            var imageSavingResult = await _imageSaver.SaveImageAsync(tag.Photo);

            if (!imageSavingResult.IsSuccessful)
            {
                return(new ErrorMessage
                {
                    ErrorCode = imageSavingResult.Error.ErrorCode,
                    Message = imageSavingResult.Error.Message,
                    StatusCode = imageSavingResult.Error.StatusCode
                }.ToCommand <Tag>());
            }


            var tagModel = new Tag
            {
                Name        = tag.TagName,
                Description = tag.Discreption,
                Photo       = new TagPhoto
                {
                    FilePath = imageSavingResult.Result.Path,
                    AdditionalInformation = QueryString.Create(new Dictionary <string, string>
                    {
                        { "Name", imageSavingResult.Result.Name.ToString() },
                        { "Version", "1" }
                    }).ToUriComponent(),
                    AddedAtUtc = DateTime.UtcNow,
                }
            };

            _tagsRepo.Add(tagModel);
            await _tagsRepo.SaveChangesAsync();

            return(new CommandResult <Tag>(tagModel));
        }