public async Task <ActionResult> AddEductionalCenter([FromForm] EductionalCenter eductionalCenter, IFormFile file) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (eductionalCenter == null) { return(NotFound()); } if (file != null) { if (file.Length == 0) { return(BadRequest("Empty file")); } if (file.Length > _photoSetting.MaxBytes) { return(BadRequest("Max file size exceeded")); } if (!_photoSetting.IsSupported(file.FileName)) { return(BadRequest("Invalid file type")); } var uploadsFolderPath = Path.Combine(_host.WebRootPath, "EductionalCenterPictures"); if (!Directory.Exists(uploadsFolderPath)) { Directory.CreateDirectory(uploadsFolderPath); } var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); var filePath = Path.Combine(uploadsFolderPath, fileName); // filepath using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); // picture saved to the path (folder) } eductionalCenter.Picture = fileName; } else { eductionalCenter.Picture = ""; // can make default picture } await _eductionalCenterRepository.AddEductionalCenter(eductionalCenter); return(Created("TeacherTable", eductionalCenter)); }
// Edit profile of // id gets from token when login, can't be able edit any profile public async Task EditEductionalCenter(EductionalCenter newEductionalCenter, int eductionalCenterId, string file) { EductionalCenter oldEductionalCenter = await GetEductionalCenterById(eductionalCenterId); oldEductionalCenter.Name = newEductionalCenter.Name; oldEductionalCenter.UserName = newEductionalCenter.UserName; oldEductionalCenter.Email = newEductionalCenter.Email; oldEductionalCenter.Password = newEductionalCenter.Password; if (file != null) { oldEductionalCenter.Picture = file; } oldEductionalCenter.About = newEductionalCenter.About; oldEductionalCenter.CityId = newEductionalCenter.CityId; oldEductionalCenter.AddressDetails = newEductionalCenter.AddressDetails; _context.Entry(oldEductionalCenter).State = EntityState.Modified; await _context.SaveChangesAsync(); }
// Add new EductionalCenter // Register public async Task AddEductionalCenter(EductionalCenter eductionalCenter) { await _context.EductionalCenters.AddAsync(eductionalCenter); await _context.SaveChangesAsync(); }
public async Task <IActionResult> EditEductionalCenter([FromForm] EductionalCenter eductionalCenter, IFormFile file) { if (!ModelState.IsValid) { ModelState.AddModelError("ErrorMessage:", "You unAuthorize to Get Data of this Teacher"); return(BadRequest(ModelState)); } var identity = User.Identity as ClaimsIdentity; if (identity == null) { ModelState.AddModelError("ErrorMessage:", "You are not Authanticated"); return(BadRequest(ModelState)); } IEnumerable <Claim> claims = identity.Claims; var eductionalCenterId = claims.Where(p => p.Type == "EductionalCenterId").FirstOrDefault()?.Value; if (eductionalCenterId == null) // check teacher is exists or no { ModelState.AddModelError("ErrorMessage:", "You are not Authanticated"); return(BadRequest(ModelState)); } var editEductionalCenterById = await _eductionalCenterRepository.GetEductionalCenterById(int.Parse(eductionalCenterId)); var fileName = editEductionalCenterById.Picture; if (int.Parse(eductionalCenterId) != 0) { if (editEductionalCenterById == null) { return(Content("not found , please Check!...")); } if (file != null) { if (file.Length == 0) { return(BadRequest("Empty file")); } if (file.Length > _photoSetting.MaxBytes) { return(BadRequest("Max file size exceeded")); } if (!_photoSetting.IsSupported(file.FileName)) { return(BadRequest("Invalid file type")); } var EductionalCenterFolderPath = Path.Combine(_host.WebRootPath, "EductionalCenterPictures"); if (!Directory.Exists(EductionalCenterFolderPath)) { Directory.CreateDirectory(EductionalCenterFolderPath); } fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); var filePath = Path.Combine(EductionalCenterFolderPath, fileName); // filepath if (editEductionalCenterById.Picture != null) { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\EductionalCenterPictures", editEductionalCenterById.Picture); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); // picture saved to the path (folder) } //teacher.Picture = fileName; } } else { using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); // picture saved to the path (folder) //teacher.Picture = fileName; } } } } // for hashing password string password = eductionalCenter.Password; byte[] salt = new byte[128 / 8]; using (var rng = RandomNumberGenerator.Create()) { rng.GetBytes(salt); } string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2( password: password, salt: salt, prf: KeyDerivationPrf.HMACSHA1, iterationCount: 10000, numBytesRequested: 256 / 8)); eductionalCenter.Password = hashed; await _eductionalCenterRepository.EditEductionalCenter(eductionalCenter, editEductionalCenterById.EductionalCenterId, fileName); return(Created("EductionalCenterTable", editEductionalCenterById)); }