Beispiel #1
0
        public async Task <IActionResult> AddAssignment([FromBody] DeviceAssignmentSaveResource saveResource)
        {
            if (!_auth.IsValidUser(User))
            {
                return(NoContent());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (await _deviceRepo.GetDevice(saveResource.DeviceId) == null)
            {
                return(BadRequest($"Device {saveResource.DeviceId} could not be found."));
            }

            if (await _assigneeRepo.GetAssignee(saveResource.AssigneeId) == null)
            {
                return(BadRequest($"Assignee {saveResource.AssigneeId} could not be found."));
            }

            var filter = new MdaAssignmentQuery()
            {
                DeviceId       = saveResource.DeviceId,
                AssignmentType = saveResource.AssignmentType,
                AssigneeId     = saveResource.AssigneeId
            };
            var assigmentFromRepo = await _repo.GetAssignments(filter);

            if (assigmentFromRepo.Any())
            {
                return(BadRequest($"Assignment already exists."));
            }

            var assignment = _mapper.Map <MdaDeviceAssignment>(saveResource);

            assignment.CreatedBy = User.Identity.Name;
            if (saveResource.StartDate == DateTime.MinValue)
            {
                assignment.StartDate = DateTime.Now;
            }

            _repo.Add(assignment);

            if (await _repo.SaveAll())
            {
                return(Ok(assignment));
            }

            return(BadRequest("Failed to add assignment"));
        }
Beispiel #2
0
        public async Task <IActionResult> UpdateAssignment(int id, [FromBody] DeviceAssignmentSaveResource saveResource)
        {
            if (!_auth.IsValidUser(User))
            {
                return(NoContent());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var assignmentFromRepo = await _repo.GetAssignment(id);

            if (assignmentFromRepo == null)
            {
                return(BadRequest($"Assignment {id} could not be found."));
            }

            if (await _deviceRepo.GetDevice(saveResource.DeviceId) == null)
            {
                return(BadRequest($"Device {saveResource.DeviceId} could not be found."));
            }

            if (await _assigneeRepo.GetAssignee(saveResource.AssigneeId) == null)
            {
                return(BadRequest($"Assignee {saveResource.AssigneeId} could not be found."));
            }

            var filter = new MdaAssignmentQuery()
            {
                DeviceId       = saveResource.DeviceId,
                AssignmentType = saveResource.AssignmentType,
                AssigneeId     = saveResource.AssigneeId
            };
            var assigmentFromRepoExisting = await _repo.GetAssignments(filter);

            if (assigmentFromRepoExisting.Any())
            {
                var existingAssignment = assigmentFromRepoExisting.FirstOrDefault();
                if (existingAssignment.Id != id)
                {
                    return(BadRequest($"Assignment already exists."));
                }
                else
                {
                    if (existingAssignment.DeviceId == saveResource.DeviceId &&
                        existingAssignment.AssignmentType == saveResource.AssignmentType &&
                        existingAssignment.AssigneeId == saveResource.AssigneeId)
                    {
                        return(BadRequest("Nothing was changed."));
                    }
                }
            }

            _mapper.Map <DeviceAssignmentSaveResource, MdaDeviceAssignment>(saveResource, assignmentFromRepo);
            assignmentFromRepo.ModifiedBy   = User.Identity.Name;
            assignmentFromRepo.ModifiedDate = DateTime.Now;
            if (saveResource.StartDate == DateTime.MinValue)
            {
                assignmentFromRepo.StartDate = DateTime.Now;
            }

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update Assignment."));
        }