Esempio n. 1
0
        public async Task <IActionResult> CreateImage(int propertyId, PropertyImageCreateRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var imageId = await _ownerPropertyService.AddImage(propertyId, request);

            if (imageId == 0)
            {
                return(BadRequest());
            }

            var image = await _ownerPropertyService.GetImageById(imageId);

            return(CreatedAtAction(nameof(GetImageById), new { id = imageId }, image));
        }
        public async Task <int> AddImage(int propertyId, PropertyImageCreateRequest request)
        {
            var propertyImage = new PropertyImage()
            {
                Caption     = request.Caption,
                DateCreated = DateTime.Now,
                IsDefault   = request.IsDefault,
                PropertyId  = propertyId,
                SortOrder   = request.SortOrder
            };

            if (request.ImageFile != null)
            {
                propertyImage.LinkName = await this.SaveFile(request.ImageFile);

                propertyImage.FileSize = request.ImageFile.Length;
            }
            _context.PropertyImages.Add(propertyImage);
            await _context.SaveChangesAsync();

            return(propertyImage.Id);
        }