public Reader AddReader(Reader reader) { var result = repo.AddReader(reader); return(cache.Set(result.Id, result)); }
private async Task <ReaderResponseModel> AddReader(ReaderRequestModel model) { var res = new ReaderResponseModel(); // magic happens here // check if model is not empty if (model != null) { // create new entity var reader = new Reader(); // add non-file attributes reader.Name = model.Name; reader.EmailAddress = model.EmailAddress; // check if any file is uploaded var work = model.Work; if (work != null) { // get the file extension and // create a new File Name using Guid var fileName = $"{Guid.NewGuid()}{Path.GetExtension(work.FileName)}"; // create full file path using // the IHostEnvironment.ContentRootPath // which is basically the execution directory // and append a sub directory workFiles // [Should be present before hand!!!] // and lastly append the file name var filePath = Path.Combine(_env.ContentRootPath, "Files", fileName); // open-create the file in a stream and // copy the uploaded file content into // the new file (IFormFile contains a stream) using (var fileSteam = new FileStream(filePath, FileMode.Create)) { await work.CopyToAsync(fileSteam); } // assign the generated filePath to the // workPath property in the entity reader.WorkPath = $"{Request.Scheme}://{Request.Host}/Files/{fileName}"; } // add the created entity to the datastore // using a Repository class IReadersRepository // which is registered as a Scoped Service // in Startup.cs var created = _repo.AddReader(reader); // Set the Success flag and generated details // to show in the View res.IsSuccess = true; res.ReaderId = created.Id.ToString(); res.WorkPath = created.WorkPath; res.RedirectTo = Url.Action("Index"); } // return the model back to view // with added changes and flags return(res); }