public async Task <IActionResult> Update([FromForm] FieldRekhankanDartaResource model) { if (model != null) { var domainField = await _uow._FiledDartaRepo.GetById(model.Id); var files = Request.Form.Files; if (files.Count > 0) { var root = Path.Combine(env.WebRootPath, "files", "field_rekhankan_darta"); if (!Directory.Exists(root)) { Directory.CreateDirectory(root); } foreach (var file in files) { var fileName = Guid.NewGuid().ToString() + "_" + Path.GetFileNameWithoutExtension(file.FileName) + "" + Path.GetExtension(file.FileName); var filePath = Path.Combine(root, fileName); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } var fieldFile = new FieldRekhankanDartaFile() { fileUrl = fileName }; var addedFile = _map.Map <RekhankanDartaFileResource> (fieldFile); model.patras.Add(new RekhankanDartaPatrasResource() { file = addedFile }); } } _map.Map <FieldRekhankanDartaResource, FieldRekhankanDarta> (model, domainField); await _uow._FiledDartaRepo.EditAsync(domainField); await _uow.CompleteAsync(); var res = _map.Map <FieldRekhankanDartaResource> (_uow._FiledDartaRepo.GetAllFieldRekhankan() .GetAwaiter().GetResult().FirstOrDefault(p => p.Id == model.Id)); return(Ok(res)); } else { return(BadRequest()); } }
public async Task <IActionResult> Post([FromForm] FieldRekhankanDartaResource model) { if (model != null) { var domainDarta = _map.Map <FieldRekhankanDarta> (model); var files = Request.Form.Files; if (files.Count > 0) { var root = Path.Combine(env.WebRootPath, "files", "field_rekhankan_darta"); if (!Directory.Exists(root)) { Directory.CreateDirectory(root); } foreach (var file in files) { var fileName = Guid.NewGuid().ToString() + "_" + Path.GetFileNameWithoutExtension(file.FileName) + "" + Path.GetExtension(file.FileName); var filePath = Path.Combine(root, fileName); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } var fieldFile = new FieldRekhankanDartaFile() { fileUrl = fileName }; domainDarta.patras.Add(new RekhankanDartaPatras() { file = fieldFile }); } } await _uow._FiledDartaRepo.CreateAsync(domainDarta); await _uow.CompleteAsync(); var res = _map.Map <FieldRekhankanDartaResource> (domainDarta); return(Ok(res)); } else { return(BadRequest()); } }