Esempio n. 1
0
        public async Task <IActionResult> Create([FromForm] PromoCodeDTO promoCode)
        {
            // if(userId != User.FindFirst(ClaimTypes.NameIdentifier).Value)
            // {
            //     return Unauthorized();
            // }
            var file = Request.Form.Files[0];

            // string fileName = file.Name;
            // string extension = Path.GetExtension(fileName);
            if (Extentions.CheckImageFileExtention(file) == false)
            {
                return(BadRequest("File format not supported"));
            }
            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation().Width(300).Height(300).Crop("fill")
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }
            // var image = new PromocodePhoto
            // {
            //     Url = promoCode.Url,
            //     PublicId = promoCode.PublicId,
            //     PromoCodeId
            // }

            promoCode.Url      = uploadResult.Uri.ToString();
            promoCode.PublicId = uploadResult.PublicId;

            var promocodeToAdd = new PromoCode
            {
                AreaManagerID = promoCode.AreaManagerID,
                Name          = promoCode.Name,
                ExpireDate    = DateTime.Now,
                IsActive      = true,
                MerchantID    = promoCode.MerchantID,
                Discount      = promoCode.Discount,
                CoinsRequired = promoCode.CoinsRequired
            };

            _context.PromoCodes.Add(promocodeToAdd);
            await _context.SaveChangesAsync();

            //For Photo

            var image = new PromocodePhoto
            {
                Url         = promoCode.Url,
                PublicId    = promoCode.PublicId,
                PromoCodeId = promocodeToAdd.Id
            };

            _context.PromocodePhotos.Add(image);
            if (await _context.SaveChangesAsync() > 0)
            {
                var photoToReturn = image;

                return(Ok(photoToReturn));
            }
            return(BadRequest());
        }