public JsonResponse<string> UploadImages(IFormFileCollection Files, int user_id, int candidate_id, Boolean isProfile) { CandidateImageLogger candidateImageLogger = new CandidateImageLogger(); JsonResponse<string> jsonResponse = new JsonResponse<string>(); string folderName; DirectoryInfo di; List<string> image_names = new List<string>(); try { using (IDbConnection dbConnection = new NpgsqlConnection(_ConnectionStringService.Value)) { dbConnection.Open(); using (var transaction = dbConnection.BeginTransaction()) { if (Files.Count > 0) { if (isProfile == true) { folderName = Path.Combine("Resources", "Images", "User", user_id.ToString(), "Candidates", candidate_id.ToString(), "Profile Image"); image_names = dbConnection.Query<string>("select image_name from candidate_image_logger where user_id = @p0 and candidate_id = @p1 and is_approved = false and is_profile_pic = true ", new { p0 = user_id, @p1 = candidate_id }).ToList(); dbConnection.Query<CandidateImageLogger>("update candidate_image_logger set is_deleted = true where user_id = @p0 and candidate_id = @p1 and is_approved = false and is_profile_pic = true ", new { p0 = user_id, @p1 = candidate_id }); } else { folderName = Path.Combine("Resources", "Images", "User", user_id.ToString(), "Candidates", candidate_id.ToString(), "Other Images"); } var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName); if (!Directory.Exists(pathToSave)) { di = Directory.CreateDirectory(pathToSave); } else { di = new DirectoryInfo(pathToSave); if (isProfile && image_names != null && image_names.Count > 0) { foreach (string image_name in image_names) { foreach (var fi in di.GetFiles()) { if (fi.Name == image_name) { fi.Delete(); break; } } } } } for (int i = 0; i < Files.Count; i++) { var file = Files[i]; if (file.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var fullPath = Path.Combine(pathToSave, fileName); var dbPath = Path.Combine(folderName, fileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } candidateImageLogger.user_id = user_id; candidateImageLogger.candidate_id = candidate_id; candidateImageLogger.image_name = file.FileName; candidateImageLogger.image_path = dbPath; candidateImageLogger.content_type = file.ContentType; candidateImageLogger.image_upload_time = DateTime.Now; candidateImageLogger.is_approved = false; candidateImageLogger.is_deleted = false; if (isProfile) { candidateImageLogger.is_profile_pic = true; candidateImageLogger.is_from_other_three_photos = false; } else { candidateImageLogger.is_from_other_three_photos = true; candidateImageLogger.is_profile_pic = false; } dbConnection.Insert<CandidateImageLogger>(candidateImageLogger, transaction); } } } transaction.Commit(); jsonResponse.Data = "Profile images uploaded successfully"; jsonResponse.IsSuccess = true; jsonResponse.Message = "Success"; } } return jsonResponse; } catch (System.Exception ex) { jsonResponse.Data = "Profile images upload fail"; jsonResponse.IsSuccess = false; jsonResponse.Message = "fail"; return jsonResponse; } }
public JsonResponse<string> updateToApproveProfile(ApprovedImageModel approveModel) { JsonResponse<string> jsonResponse = new JsonResponse<string>(); string folderName; DirectoryInfo di; try { using (IDbConnection dbConnection = new NpgsqlConnection(_ConnectionStringService.Value)) { dbConnection.Open(); using (var transaction = dbConnection.BeginTransaction()) { CandidateImageLogger imageDetails = dbConnection.Query<CandidateImageLogger>("select * from candidate_image_logger where id = @p0", new { p0 = approveModel.imageLoggedId }).FirstOrDefault(); if (imageDetails.is_profile_pic == true) { folderName = Path.Combine("Resources", "Images", "User", imageDetails.user_id.ToString(), "Candidates", imageDetails.candidate_id.ToString(), "Profile Image"); var path = Path.Combine(Directory.GetCurrentDirectory(), folderName); if (Directory.Exists(path)) { di = new DirectoryInfo(path); foreach (FileInfo fi in di.GetFiles()) { if (fi.Name != imageDetails.image_name) { fi.Delete(); } } } dbConnection.Query<CandidateImageLogger>("update candidate_image_logger set is_deleted = true where user_id = @p0 and candidate_id = @p1 and is_approved = true and is_profile_pic = true and is_deleted = false and id != @p2", new { p0 = imageDetails.user_id, @p1 = imageDetails.candidate_id, @p2 = imageDetails.id }); dbConnection.Query<CandidateImageLogger>("update candidate_image_logger set is_approved = true , is_profile_pic = true where id = @p0", new { p0 = approveModel.imageLoggedId }); } else if (imageDetails.is_from_other_three_photos == true) { dbConnection.Query<CandidateImageLogger>("update candidate_image_logger set is_approved = true where id = @p0", new { p0 = approveModel.imageLoggedId }); } transaction.Commit(); } } jsonResponse.IsSuccess = true; jsonResponse.Message = "success"; return jsonResponse; } catch (Exception e) { jsonResponse.IsSuccess = false; jsonResponse.Message = "fail"; return jsonResponse; } }