Esempio n. 1
0
        public async Task <IActionResult> UpdateTrade(int id, [FromBody] SaveTradeResource tradeResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var trade = await repository.GetTradeAsync(id);

            if (trade == null)
            {
                return(NotFound());
            }

            mapper.Map <SaveTradeResource, Trade>(tradeResource, trade);

            await unitOfWork.CompleteAsync();

            var result = mapper.Map <Trade, LoadTradeResource>(trade);

            return(Ok(result));
        }
Esempio n. 2
0
        public async Task <IActionResult> Upload(int tradeId, IFormFile file)
        {
            var trade = await repository.GetTradeAsync(tradeId, includeRelated : false);

            if (trade == null)
            {
                return(NotFound());
            }

            if (file == null)
            {
                return(BadRequest("Null file"));
            }
            if (file.Length == 0)
            {
                return(BadRequest("Empty file"));
            }
            if (file.Length >= photoSettings.MaxBytes)
            {
                return(BadRequest("Exceeds file limit"));
            }
            if (!photoSettings.IsSupported(file.FileName))
            {
                return(BadRequest("file formats not supported"));
            }

            var uploadsFolderPath = Path.Combine(host.WebRootPath, "uploads");

            if (!Directory.Exists(uploadsFolderPath))
            {
                Directory.CreateDirectory(uploadsFolderPath);
            }

            var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
            var filePath = Path.Combine(uploadsFolderPath, fileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            var photo = new Photo {
                FileName = fileName
            };

            trade.Photos.Add(photo);
            await unitOfWork.CompleteAsync();

            return(Ok(mapper.Map <Photo, PhotoResource>(photo)));
        }