Ejemplo n.º 1
0
        public async Task <DiaperChange> CreateDiaperChange(PostDiaperChanges diaperChangesDTO, int babyId)
        {
            if (diaperChangesDTO == null)
            {
                throw new ArgumentNullException(nameof(diaperChangesDTO));
            }

            var babies = unitOfWork.Babies.GetAll();
            var baby   = babies.FirstOrDefault(
                baby => baby.Id == babyId);

            var diaperChange = new DiaperChange()
            {
                Name       = diaperChangesDTO.Name,
                TimeDiaper = diaperChangesDTO.TimeDiaper,
                Reason     = diaperChangesDTO.Reason,
            };

            if (baby != null)
            {
                diaperChange.BabyId = babyId;
                diaperChange.Baby   = baby;

                unitOfWork.DiaperChanges.Create(diaperChange);
                await unitOfWork.SaveChangesAsync();

                return(diaperChange);
            }

            return(null);
        }
        public async Task <IActionResult> PostDiaperChange([FromBody] PostDiaperChanges diaperChangesDTO, [FromRoute] int babyId)
        {
            var result = await diaperChangesService.CreateDiaperChange(diaperChangesDTO, babyId);

            if (result == null)
            {
                return(NotFound());
            }

            return(CreatedAtAction("GetDiaperChange", new { id = result.Id }, result));
        }