コード例 #1
0
        public async Task <IActionResult> Upload(int vechileId, IFormFile file)
        {
            var vechile = await repository.GetVechile(vechileId, includeRelated : false);

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

            if (file == null)
            {
                return(BadRequest("Null file"));
            }
            if (file.Length == 0)
            {
                return(BadRequest("Empty file"));
            }
            if (file.Length > MAX_BYTES)
            {
                return(BadRequest("Max file size exceeded |current: " + file.Length + " |max: " + MAX_BYTES));
            }
            if (!ACCEPTED_FILE_TYPES.Any(s => s == Path.GetExtension(file.FileName)))
            {
                return(BadRequest("Invalid file type"));
            }

            // wwwroot/uploads
            var uploadsFolderPath = Path.Combine(host.WebRootPath, "uploads");
            var photo             = await photoService.UploadPhoto(vechile, file, uploadsFolderPath);

            return(Ok(Mapper.Map <Photo, PhotoResource>(photo)));
        }
コード例 #2
0
ファイル: VechilesController.cs プロジェクト: polkrt/Vega
        public async Task <IActionResult> CreateVechile([FromBody] SaveVechileResource vechileResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var vechile = mapper.Map <SaveVechileResource, Vechile>(vechileResource);

            vechile.LastUpdate = DateTime.Now;

            repository.Add(vechile);
            await unitOfWork.CompleteAsync();

            vechile = await repository.GetVechile(vechile.Id);

            var result = mapper.Map <Vechile, VechileResource>(vechile);

            return(Ok(result));
        }