Ejemplo n.º 1
0
        public ServiceResponse Delete(int id)
        {
            using (DiaryContext db = new DiaryContext())
            {
                if (!db.Days.Any(x => x.Id == id))
                {
                    return(new ServiceResponse(HttpStatusCode.NotFound, "There is not existing day with given id!"));
                }

                var             dayToRemove     = db.Days.Include("Diary").Include("Dream").Include("TrainingUnits").FirstOrDefault(x => x.Id == id);
                TrainingService trainingService = new TrainingService();
                foreach (var trainingUnit in dayToRemove.TrainingUnits)
                {
                    trainingService.DeleteTrainingUnit(trainingUnit.Id);
                }
                if (dayToRemove.Dream != null)
                {
                    db.Dreams.Remove(dayToRemove.Dream);
                }
                db.SaveChanges();
            }
            using (DiaryContext db = new DiaryContext())
            {
                var dayToRemove2 = db.Days.Include("Diary").Include("Dream").Include("TrainingUnits").FirstOrDefault(x => x.Id == id);
                db.Days.Remove(dayToRemove2);
                db.SaveChanges();
            }
            return(new ServiceResponse(HttpStatusCode.OK, "Day deleted!"));
        }
Ejemplo n.º 2
0
        public ServiceResponse DeleteExercise(int id)
        {
            using (DiaryContext db = new DiaryContext())
            {
                if (!db.Exercises.Any(x => x.Id == id))
                {
                    return(new ServiceResponse(HttpStatusCode.NotFound, "There is not existing exercise with given id!"));
                }

                var             exerciseToRemove = db.Exercises.Include("ExerciseEquipment").Include("ExerciseTrainings").FirstOrDefault(x => x.Id == id);
                TrainingService trainingService  = new TrainingService();
                foreach (var exerciseTraining in exerciseToRemove.ExerciseTrainings)
                {
                    trainingService.DeleteExerciseTraining(exerciseTraining.Id);
                }
                db.Exercises.Remove(exerciseToRemove);
                db.SaveChanges();
            }
            return(new ServiceResponse(HttpStatusCode.OK, "Exercise deleted!"));
        }