public IActionResult EditRoutine(int routineId, DtoEditRoutine newRoutine)
 {
     try
     {
         var x          = int.Parse(User.Identity?.Name ?? throw new InvalidOperationException());
         var routineDto = _routineHelper.EditRoutine(routineId, newRoutine, x);
         return(Ok(routineDto));
     }
     catch
     {
         return(Conflict());
     }
 }
        public int EditRoutine(int routineId, DtoEditRoutine newRoutine, int userId)
        {
            var routineExists = _context.Routines
                                .Where(o => o.UserId == userId)
                                .Any(o => o.Id == routineId);

            if (!routineExists)
            {
                throw new NotFoundException();
            }

            var newDate = newRoutine.RoutineDate.AddDays(1);
            var note    = _context.Notes
                          .Where(o => o.Date >= newRoutine.RoutineDate)
                          .Where(o => o.Date < newDate)
                          .FirstOrDefault(o => o.RoutineId == routineId);

            if (note != null)
            {
                note.Text = newRoutine.Note;
            }
            else
            {
                var newNote = new Note();
                newNote.Text      = newRoutine.Note;
                newNote.Date      = newRoutine.RoutineDate;
                newNote.RoutineId = routineId;
                _context.Add(newNote);
            }

            var indicators = _context.Indicators
                             .Where(o => o.RoutineId == routineId)
                             .Where(o => o.Date >= newRoutine.RoutineDate)
                             .Where(o => o.Date < newDate)
                             .ToList();

            var oldWater = indicators
                           .FirstOrDefault(o => o.IndicatorTypeId == 1);

            if (oldWater != null)
            {
                oldWater.Value = newRoutine.Water;
            }
            else
            {
                var waterIndicator = new Indicator();
                waterIndicator.Date            = newRoutine.RoutineDate;
                waterIndicator.Value           = newRoutine.Water;
                waterIndicator.IndicatorTypeId = 1;
                waterIndicator.RoutineId       = routineId;
                _context.Add(waterIndicator);
            }

            var oldStress = indicators
                            .FirstOrDefault(o => o.IndicatorTypeId == 2);

            if (oldStress != null)
            {
                oldStress.Value = newRoutine.Stress switch
                {
                    "bad" => 3,
                    "normal" => 2,
                    _ => 1
                };
            }
            else
            {
                var stressIndicator = new Indicator();
                stressIndicator.RoutineId = routineId;
                stressIndicator.Date      = newRoutine.RoutineDate;
                stressIndicator.Value     = newRoutine.Stress switch
                {
                    "bad" => 3,
                    "normal" => 2,
                    _ => 1
                };
                stressIndicator.IndicatorTypeId = 2;
                _context.Add(stressIndicator);
            }

            var oldSleeping = indicators
                              .FirstOrDefault(o => o.IndicatorTypeId == 3);


            var x        = newRoutine.WakeUp - newRoutine.GoToSleep;
            var sleeping = x?.TotalHours != null ? (float)x.Value.TotalHours : 0;

            if (oldSleeping != null)
            {
                oldSleeping.Value = sleeping;
            }
            else
            {
                var sleepingIndicator = new Indicator();
                sleepingIndicator.RoutineId       = routineId;
                sleepingIndicator.Date            = newRoutine.RoutineDate;
                sleepingIndicator.Value           = sleeping;
                sleepingIndicator.IndicatorTypeId = 3;
                _context.Add(sleepingIndicator);
            }

            EditProduct(newRoutine.Cleanser, 1, routineId);
            EditProduct(newRoutine.Treatment, 2, routineId);
            EditProduct(newRoutine.Moisturizer, 3, routineId);
            EditProduct(newRoutine.SunScreen, 4, routineId);
            EditProduct(newRoutine.Other, 5, routineId);
            _context.SaveChanges();

            return(routineId);
        }
    }