//[HttpGet] //public IActionResult UpdateChild(int childId) //{ // var viewModel = new IndexViewModel(); // if (!ModelState.IsValid) // { return BadRequest(ModelState); } // ViewBag.firstname = viewModel.FirstName; // var childFromDb = _iRepo.GetChildById(childId, false, false); // var childNewData = new ChildDto() // { // FirstName = firstName, // LastName = lastName, // DateOfBirth = dateOfBirth // }; // var childToCreate = AutoMapper.Mapper.Map<Domain.Child>(child); // return PartialView("Index"); //} //[HttpPut] public IActionResult UpdateChild(int childId, string firstName, string lastName, DateTime dateOfBirth) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_iRepo.ChildExists(childId)) { return(NotFound()); } var childFromDb = _iRepo.GetChildById(childId, false, false); var childNewData = new ChildDto() { Id = childId, FirstName = firstName, LastName = lastName, DateOfBirth = dateOfBirth }; AutoMapper.Mapper.Map(childNewData, childFromDb); if (!_iRepo.Save()) { return(StatusCode(500, "A problem happend while saving your request.")); } return(Ok("Updaterad")); }
public IActionResult GetChildById(int id, bool includePlayEvents = false, bool includeSleepingPeriods = false) { var child = _iRepo.GetChildById(id, includePlayEvents, includeSleepingPeriods); if (child == null) { return(NotFound()); } if (includePlayEvents) { var childResult = AutoMapper.Mapper.Map <ChildDto>(child); return(new JsonResult(childResult)); } if (includeSleepingPeriods) { //var childResult = new ChildDto() //{ // Id = child.Id, // DateOfBirth = child.DateOfBirth, // FirstName = child.FirstName, // LastName = child.LastName //}; //foreach (var sPer in child.SleepingPeriods) //{ // childResult.ChildrensPlayEvents.Add( // new SleepingPeriodDto() // { // Id = sPer.Id, // TypeOfSleepingPeriod = sPer.TypeOfSleepingPeriod as TypeOfSleepingPeriods, // From = sPer.From, // To = sPer.To // }); //} var childResult = AutoMapper.Mapper.Map <ChildDto>(child); return(new JsonResult(childResult)); } else { var childOnlyResult = AutoMapper.Mapper.Map <ChildOnlyDto>(child); return(new JsonResult(childOnlyResult)); } }
public IActionResult CreateSleepingPeriod(int childId, DateTime from, DateTime to) { //if(sleepingPeriod == null) //{ // return BadRequest(); //} if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var child = _iRepo.GetChildById(childId, false, false); if (child == null) { return(NotFound()); } var sleepingPeriod = new SleepingPeriodForCreationDto() { ChildId = childId, Child = child, From = from, To = to, TypeOfSleepingPeriod = SleepingPeriodForCreationDto.TypeOfSleepingPeriods.alright }; var sleepingPeriodToCreate = AutoMapper.Mapper.Map <Domain.SleepingPeriod>(sleepingPeriod); _iRepo.AddSleepingPeriodByChildId(childId, sleepingPeriodToCreate); if (!_iRepo.Save()) { return(StatusCode(500, "A problem happend while saving your request.")); } var createdSleepingPeriod = AutoMapper.Mapper.Map <Models.SleepingPeriodDto>(sleepingPeriodToCreate); return(Ok(createdSleepingPeriod)); }