Example #1
0
        public async Task <IActionResult> CreateCareEvent(
            [FromBody] CareEventForUpdateDto careEventForUpdate)
        {
            if (careEventForUpdate == null)
            {
                ModelState.AddModelError("Message", "Unable to locate payload for new request");
                return(BadRequest(ModelState));
            }

            if (Regex.Matches(careEventForUpdate.CareEventName, @"[a-zA-Z ']").Count < careEventForUpdate.CareEventName.Length)
            {
                ModelState.AddModelError("Message", "Description contains invalid characters (Enter A-Z, a-z)");
                return(BadRequest(ModelState));
            }

            if (_unitOfWork.Repository <CareEvent>().Queryable().
                Where(l => l.Description == careEventForUpdate.CareEventName)
                .Count() > 0)
            {
                ModelState.AddModelError("Message", "Item with same name already exists");
                return(BadRequest(ModelState));
            }

            long id = 0;

            if (ModelState.IsValid)
            {
                var newCareEvent = new CareEvent()
                {
                    Description = careEventForUpdate.CareEventName
                };

                _careEventRepository.Save(newCareEvent);
                id = newCareEvent.Id;
            }

            var mappedCareEvent = await GetCareEventAsync <CareEventIdentifierDto>(id);

            if (mappedCareEvent == null)
            {
                return(StatusCode(500, "Unable to locate newly added item"));
            }

            return(CreatedAtAction("GetCareEventByIdentifier",
                                   new
            {
                id = mappedCareEvent.Id
            }, CreateLinksForCareEvent <CareEventIdentifierDto>(mappedCareEvent)));
        }
Example #2
0
        public async Task <IActionResult> UpdateCareEvent(long id,
                                                          [FromBody] CareEventForUpdateDto careEventForUpdate)
        {
            if (careEventForUpdate == null)
            {
                ModelState.AddModelError("Message", "Unable to locate payload for new request");
                return(BadRequest(ModelState));
            }

            if (Regex.Matches(careEventForUpdate.CareEventName, @"[a-zA-Z ']").Count < careEventForUpdate.CareEventName.Length)
            {
                ModelState.AddModelError("Message", "Description contains invalid characters (Enter A-Z, a-z)");
                return(BadRequest(ModelState));
            }

            if (_unitOfWork.Repository <CareEvent>().Queryable().
                Where(l => l.Description == careEventForUpdate.CareEventName && l.Id != id)
                .Count() > 0)
            {
                ModelState.AddModelError("Message", "Item with same name already exists");
                return(BadRequest(ModelState));
            }

            var careEventFromRepo = await _careEventRepository.GetAsync(f => f.Id == id);

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

            if (ModelState.IsValid)
            {
                careEventFromRepo.Description = careEventForUpdate.CareEventName;

                _careEventRepository.Update(careEventFromRepo);
                await _unitOfWork.CompleteAsync();
            }

            return(Ok());
        }