// Update (Create) public static ResponseResult Update(QuestionViewModel entity) { ResponseResult result = new ResponseResult(); try { using (var db = new XBC_Context()) { if (entity.id == 0) // Create { t_question qs = new t_question(); qs.question_type = entity.questionType; qs.question = entity.question; qs.image_url = entity.imageUrl; qs.created_by = entity.UserId; qs.created_on = DateTime.Now; qs.is_deleted = false; db.t_question.Add(qs); db.SaveChanges(); // Audit Log Insert var json = new JavaScriptSerializer().Serialize(qs); t_audit_log log = new t_audit_log(); log.type = "INSERT"; log.json_insert = json; log.created_by = entity.UserId; log.created_on = DateTime.Now; db.t_audit_log.Add(log); db.SaveChanges(); object data = new { qs.id, qs.question, qs.question_type, qs.image_url }; entity.id = qs.id; result.Entity = data; } } } catch (Exception ex) { result.Success = false; result.ErrorMessage = ex.Message; } return(result); }
public static ResponResultViewModel Update(QuestionViewModel entity, long userid) { //Untuk create dan edit ResponResultViewModel result = new ResponResultViewModel(); try { using (var db = new MinProContext()) { //Create if (entity.id == 0) { t_question question = new t_question(); question.question = entity.question; question.created_by = userid; question.created_on = DateTime.Now; question.is_delete = false; db.t_question.Add(question); db.SaveChanges(); result.Entity = question; var json_insert = new JavaScriptSerializer().Serialize(question); AuditRepo.Insert(json_insert, userid); } //Delete else { t_question question = db.t_question.Where(o => o.id == entity.id).FirstOrDefault(); if (question != null) { t_question json_before1 = new t_question(); json_before1.id = question.id; json_before1.question = question.question; json_before1.created_by = question.created_by; json_before1.created_on = question.created_on; var json_before = new JavaScriptSerializer().Serialize(json_before1); question.question = entity.question; question.deleted_by = userid; question.deleted_on = DateTime.Now; question.is_delete = true; db.SaveChanges(); result.Entity = entity; var json_after = new JavaScriptSerializer().Serialize(question); AuditRepo.Modify(json_after, json_before, userid); } else { result.Success = false; result.Message = "Category not Found!"; } } } } catch (Exception ex) { result.Success = false; result.Message = ex.Message; } return(result); }
//Delete public static ResponseResult Delete(QuestionViewModel entity) { ResponseResult result = new ResponseResult(); try { using (var db = new XBC_Context()) { t_question qs = db.t_question.Where(o => o.id == entity.id).FirstOrDefault(); if (qs != null) { var Serial = new JavaScriptSerializer(); object dataBefore = new //Mengambil Data Before for Log { qs.option_a, qs.option_b, qs.option_c, qs.option_d, qs.option_e, qs.image_a, qs.image_b, qs.image_c, qs.image_d, qs.image_e, qs.is_deleted }; qs.deleted_by = entity.UserId; qs.deleted_on = DateTime.Now; qs.is_deleted = true; db.SaveChanges(); // Audit Log Modify object dataAfter = new { qs.option_a, qs.option_b, qs.option_c, qs.option_d, qs.option_e, qs.image_a, qs.image_b, qs.image_c, qs.image_d, qs.image_e, qs.is_deleted }; t_audit_log log = new t_audit_log(); log.type = "MODIFY"; log.json_before = Serial.Serialize(dataBefore); log.json_after = Serial.Serialize(dataAfter); log.created_by = entity.UserId; log.created_on = DateTime.Now; db.t_audit_log.Add(log); db.SaveChanges(); result.Entity = entity; } else { result.Success = false; result.ErrorMessage = "Question Not Found"; } } } catch (Exception ex) { result.Success = false; result.ErrorMessage = ex.Message; } return(result); }