public IActionResult CreateRoutine(DtoNewRoutine newRoutine) { if (User.Identity != null) { var x = int.Parse(User.Identity.Name ?? throw new InvalidOperationException()); var routineDto = _routineHelper.CreateRoutine(newRoutine, x); if (routineDto != null) { return(Ok(routineDto)); } } return(Conflict()); }
public int CreateRoutine(DtoNewRoutine routine, int userId) { var idTypeOfId = routine.RoutineType switch { "Morning" => 1, "Evening" => 2, _ => 3 }; var newNote = new Note(); newNote.Text = routine.Note; newNote.Date = routine.RoutineDate; var newRoutineDate = new RoutineDate(); newRoutineDate.Start = routine.RoutineDate; if (routine.RoutineEndDate == null) { newRoutineDate.End = routine.RoutineDate; } if (routine.RoutineEndDate != null) { newRoutineDate.End = routine.RoutineEndDate.Value; } if (routine.DayOfWeek != null) { newRoutineDate.Mon = routine.DayOfWeek["mon"]; newRoutineDate.Tue = routine.DayOfWeek["tue"]; newRoutineDate.Wed = routine.DayOfWeek["wed"]; newRoutineDate.Thu = routine.DayOfWeek["thu"]; newRoutineDate.Fri = routine.DayOfWeek["fri"]; newRoutineDate.Sat = routine.DayOfWeek["sat"]; newRoutineDate.Sun = routine.DayOfWeek["sun"]; } else { newRoutineDate = null; } var waterIndicator = new Indicator(); waterIndicator.Date = routine.RoutineDate; waterIndicator.Value = routine.Water; waterIndicator.IndicatorTypeId = 1; var stressIndicator = new Indicator(); stressIndicator.Date = routine.RoutineDate; stressIndicator.Value = routine.Stress switch { "bad" => 3, "normal" => 2, _ => 1 }; stressIndicator.IndicatorTypeId = 2; var sleepingIndicator = new Indicator(); sleepingIndicator.Date = routine.RoutineDate; var x = routine.WakeUp - routine.GoToSleep; var sleeping = x?.TotalHours; if (sleeping != null) { sleepingIndicator.Value = (float)(sleeping); } sleepingIndicator.IndicatorTypeId = 3; var newRoutine = new Routine(); newRoutine.TypeOfRoutineId = idTypeOfId; newRoutine.Notes = new List <Note>(); newRoutine.Notes.Add(newNote); newRoutine.UserId = userId; newRoutine.RoutineDate = newRoutineDate; newRoutine.Indicators = new List <Indicator>(); newRoutine.Indicators.Add(waterIndicator); newRoutine.Indicators.Add(stressIndicator); newRoutine.Indicators.Add(sleepingIndicator); _context.Routines.Add(newRoutine); _context.SaveChanges(); AddProduct(routine.Cleanser, 1, newRoutine.Id); AddProduct(routine.Treatment, 2, newRoutine.Id); AddProduct(routine.Moisturizer, 3, newRoutine.Id); AddProduct(routine.SunScreen, 4, newRoutine.Id); AddProduct(routine.Other, 5, newRoutine.Id); return(newRoutine.Id); }
public void CreateRoutineTest() { var optionsBuilder = new DbContextOptionsBuilder <TestRepositoryContext>(); optionsBuilder.UseInMemoryDatabase("loginManagerTestDb"); using var context = new TestRepositoryContext(optionsBuilder.Options); var routine = new RoutineHelper(context); List <DtoProductsFromNewRoutine> cleanser = new List <DtoProductsFromNewRoutine>(); var avon = new DtoProductsFromNewRoutine() { Id = 1, Name = "Avon" }; cleanser.Add(avon); List <DtoProductsFromNewRoutine> treatment = new List <DtoProductsFromNewRoutine>(); var ordinary = new DtoProductsFromNewRoutine() { Id = 5, Name = "Ordinary" }; treatment.Add(ordinary); var inkeyList = new DtoProductsFromNewRoutine() { Id = 6, Name = "InkeyList" }; treatment.Add(inkeyList); List <DtoProductsFromNewRoutine> other = new List <DtoProductsFromNewRoutine>(); var balea = new DtoProductsFromNewRoutine() { Id = 2, Name = "Balea" }; other.Add(balea); var ticks = new DateTime(2021, 07, 28, 22, 35, 5, new CultureInfo("en-US", false).Calendar).Ticks; DateTime routineDate = new DateTime(ticks); /*public string RoutineType { get; set; } * public string Note { get; set; } * public List<string> Photos { get; set; } * public string Stress { get; set; } * public float Water { get; set; } * public DateTime? GoToSleep { get; set; } * public DateTime? WakeUp { get; set; } * public DateTime RoutineDate { get; set; } * public DateTime? RoutineEndDate { get; set; } * public Dictionary<string, bool> DayOfWeek { get; set; } * public int AmountOfWeek { get; set; } * * public List<DtoProductsFromNewRoutine> Cleanser { get; set; } * public List<DtoProductsFromNewRoutine> Treatment { get; set; } * public List<DtoProductsFromNewRoutine> Moisturizer { get; set; } * public List<DtoProductsFromNewRoutine> SunScreen { get; set; } * public List<DtoProductsFromNewRoutine> Other { get; set; }*/ var dtoRoutine = new DtoNewRoutine() { RoutineType = "Morning", Note = "Note", Photos = null, RoutineDate = routineDate, Cleanser = cleanser, Treatment = treatment, Other = other, Stress = "bad", Water = 3, GoToSleep = null, WakeUp = null, AmountOfWeek = 0, SunScreen = null, RoutineEndDate = null, DayOfWeek = null, Moisturizer = null, }; var pokus = routine.CreateRoutine(dtoRoutine, 1); Assert.Equal(1, routine.CreateRoutine(dtoRoutine, 1)); }