// [HttpPut("{id}")] public async Task <IActionResult> UpdateConsultant(int Id, [FromBody] ConsultantsVM consultant) { try { if (consultant == null) { return(BadRequest(new { errorMessage = "object cannot be null" })); } if (!ModelState.IsValid) { return(BadRequest(new { errorMessage = "Invalid object" })); } //get the customer's record var existingRecord = await _repoWrapper.Consultants.GetConsultantByIdAsync(Id); if (existingRecord == null) { return(NotFound()); } //map the incoming data to the data fetched from the db _mapper.Map(consultant, existingRecord); _repoWrapper.Consultants.UpdateConsultant(existingRecord); await _repoWrapper.SaveAsync(); return(NoContent());//success } catch (Exception) { return(this.StatusCode(500)); } }
public async Task <IActionResult> CreateConsultant([FromForm] ConsultantsVM consultant) { try { //check if profile picture is present // var profilePicture = consultant.ProfilePicture; // if (profilePicture.Length <= 0) return BadRequest(new { errorMessage = "Profile picture must be uploaded" }); //check if customer already exists var existingConsultant = await _repoWrapper.Consultants.GetConsultantByEmailAsync(consultant.Email); if (existingConsultant != null) { return(BadRequest(new { errorMessage = "Record already exists" })); } //check if customer object is null if (consultant == null) { return(BadRequest(new { errorMessage = "consultant object can not be null" })); } if (!ModelState.IsValid) { return(BadRequest(new { errorMessage = "Invalid consultant model" })); } //give the file a unique id // Guid uniqueFileId = Guid.NewGuid(); //give file a unique name to prevent overwriting //var uniqueFileName = uniqueFileId + profilePicture.FileName.Replace(" ", "_");//replace spaces with underscore //specify folder to save uploaded files // var filePath = Path.Combine("Uploads/Images", uniqueFileName); //this works, but saves file inside server, not a specific folder // using(var fileStream = new FileStream(profilePicture.FileName, FileMode.Create)) //save profile picture // using (var fileStream = new FileStream(filePath, FileMode.Create)) //{ // await profilePicture.CopyToAsync(fileStream); //} //map incoming customer object of CustomerCreationDto to the Customer model class var consultantEntity = _mapper.Map <Consultant>(consultant); //add the unique filename to customerentity object and save to db // consultantEntity.ProfilePicture = uniqueFileName; //take out consultantEntity.ProfilePicture = "test"; consultantEntity.ZoomId = "test"; consultantEntity.SkypeId = "test"; //end _repoWrapper.Consultants.CreateConsultant(consultantEntity); await _repoWrapper.SaveAsync(); return(Created($"/api/consultants/{consultantEntity.Id}", consultantEntity)); } catch (Exception) { //if an exception occurs return(this.StatusCode(501)); } }