Beispiel #1
0
        public async Task <ActionResult <WeightHistoriesDto> > AddWeightHistoryForMember(string memberId, [FromBody] WeightHistoriesCreationDto weightHistoryDTO)
        {
            if (weightHistoryDTO == null)
            {
                return(BadRequest());
            }

            var user = await _memberManager.FindByIdAsync(memberId);

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

            var weightToAdd = _mapper.Map <WeightHistories>(weightHistoryDTO);

            user.WeightHistories.Add(weightToAdd);

            var Id = await _memberManager.UpdateAsync(user);

            if (!Id.Succeeded)
            {
                throw new Exception("Creation of diet tracker failed");
            }


            var weightToReturn = _mapper.Map <WeightHistoriesDto>(weightHistoryDTO);

            return(CreatedAtRoute("GetWeightHistoryForMember", new { memberId = memberId, Id = Id }, weightToReturn));
        }
Beispiel #2
0
        public async Task <ActionResult <WeightHistoriesDto> > UpdateWeightHistoryForMember(string memberId, int id, [FromBody] WeightHistoriesCreationDto weightHistoryDTO)
        {
            if (weightHistoryDTO == null)
            {
                return(BadRequest());
            }

            var user = await _memberManager.FindByIdAsync(memberId);

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

            var weightForMember = await _weightHistoryRepository.GetFirstAsync(r => r.UserId == user.Id && r.Id == id);

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

            _mapper.Map(weightHistoryDTO, weightForMember);

            try
            {
                await _weightHistoryRepository.UpdateAsync(weightForMember);
            }
            catch (Exception)
            {
                throw new Exception("update weight failed");
            }

            return(NoContent());
        }