/// <summary> /// Create data in database /// </summary> /// <param name="muscle">Data</param> /// <returns>insert data</returns> public Muscle Create(Muscle muscle) { if (muscle == null) return null; if (muscle.Id == 0) { var key = new MuscleKey(); var sequencerManager = new SequencerManager(); do { key.Id = sequencerManager.GetNextValue(_dbContext, 4, "muscle"); } while (Get(key) != null); // Test Record exist muscle.Id = key.Id; } if (muscle.Id == 0) return null; var row = new MuscleRow(); MuscleTransformer.ToRow(muscle, row); _dbContext.Muscle.Add(row); _dbContext.SaveChanges(); return MuscleTransformer.ToBean(row); }
public static void ToRow(Muscle bean, MuscleRow row) { if (bean == null) return; row.Id = bean.Id; row.MuscularGroupId = bean.MuscularGroupId; }
private void CompleteTranslation(Muscle muscle) { if (muscle != null) { string trKey = MuscleTransformer.GetTranslationKey(muscle.Id); muscle.Name = Translation.GetInDB(trKey, DbContext); } }
internal Muscle UpdateMuscle(Muscle muscle) { SaveTranslation(muscle); Muscle result = _muscleModule.Update(muscle); CompleteTranslation(result); return result; }
internal static Muscle ToBean(MuscleRow row) { if (row == null) return null; var bean = new Muscle(); bean.Id = row.Id; bean.MuscularGroupId = row.MuscularGroupId; return bean; }
internal Muscle CreateMuscle(Muscle muscle) { string name = muscle != null ? muscle.Name : null; var result = _muscleModule.Create(muscle); if(result != null && result.Id != 0) { result.Name = name; SaveTranslation(result); CompleteTranslation(result); } return result; }
public IActionResult Create(MuscleViewModel viewModel) { if (ModelState.IsValid) { var muscle = new Muscle() { Name = viewModel.Name, MuscularGroupId = viewModel.MuscularGroupId }; muscle = _musclesService.CreateMuscle(muscle); if (muscle == null || muscle.Id == 0) { _logger.LogError("Create new muscle fail"); } return RedirectToAction("Index"); } ViewBag.MuscularGroups = ControllerUtils.CreateSelectMuscularGroupItemList(_muscularGroupsService.FindMuscularGroups(), 0); return View(viewModel); }
public Muscle CreateMuscle(Muscle muscle) { Muscle result = null; BeginTransaction(); try { result = GetMuscleManager().CreateMuscle(muscle); CommitTransaction(); //invalidate cache InvalidateCache(_cacheName); } catch (Exception exception) { _logger.LogCritical("Unable to create muscle", exception); RollbackTransaction(); throw exception; } finally { EndTransaction(); } return result; }
private void SaveTranslation(Muscle muscle) { if (muscle != null) { string trKey = BodyExerciseTransformer.GetTranslationKey(muscle.Id); Translation.UpdateInDB(trKey, muscle.Name, DbContext); } }
/// <summary> /// Update data in database /// </summary> /// <param name="muscle">data</param> /// <returns>updated data</returns> public Muscle Update(Muscle muscle) { if (muscle == null || muscle.Id == 0) return null; var row = _dbContext.Muscle.Where(m => m.Id == muscle.Id).FirstOrDefault(); if (row == null) { // No data in database return Create(muscle); } else { //Modify Data in database MuscleTransformer.ToRow(muscle, row); _dbContext.SaveChanges(); return MuscleTransformer.ToBean(row); } }