public static void FromRequest(this PhysiologicalSignal physiologicalSignal, PhysiologicalSignalRequest request)
 {
     physiologicalSignal.ParticipantId = request.ParticipantId;
     physiologicalSignal.Timestamp     = request.Timestamp;
     physiologicalSignal.Type          = request.Type;
     physiologicalSignal.Value         = request.Value;
     physiologicalSignal.Accuracy      = request.Accuracy;
 }
        public async Task <IActionResult> CreatePhysiologicalSignal([FromBody] PhysiologicalSignalRequest physiologicalSignalRequest)
        {
            if (physiologicalSignalRequest == null)
            {
                _logger.LogError("CreatePhysiologicalSignal: PhysiologicalSignalRequest object sent from client is null.");
                return(BadRequest("PhysiologicalSignalRequest object is null"));
            }
            if (!ModelState.IsValid)
            {
                _logger.LogError("CreatePhysiologicalSignal: Invalid PhysiologicalSignalRequest object sent from client.");
                return(BadRequest("Invalid PhysiologicalSignalRequest object"));
            }
            string userId = HttpContext.User.Claims.Single(x => x.Type == "id").Value;
            string role   = HttpContext.User.Claims.Single(x => x.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role").Value;

            if (role == Role.SubAdministratorRole)
            {
                if (!await ParticipantInOrganizationOfUserIdAsync(physiologicalSignalRequest.ParticipantId, userId))
                {
                    return(BadRequest("A sub-administrator can create only psychological signals of a participant of own organization"));
                }
            }
            else if (role == Role.SupervisorRole)
            {
                if (!await ParticipantInStudiesOfUserIdAsync(physiologicalSignalRequest.ParticipantId, userId))
                {
                    return(BadRequest("A supervisor can create only psychological signals of a participant of own studies"));
                }
            }
            else if (role == Role.ParticipantRole)
            {
                if (!await ParticipantSameAsUserIdAsync(physiologicalSignalRequest.ParticipantId, userId))
                {
                    return(BadRequest("A participant can create only own psychological signals"));
                }
            }
            else if (role == Role.TherapistRole)
            {
                var participant = await _coadaptService.Participant.GetParticipantByIdAsync(physiologicalSignalRequest.ParticipantId);

                if (!await ParticipantMonitoredByTherapistOfUserIdAsync(participant, userId))
                {
                    return(BadRequest("A therapist can create only psychological signals of monitored participants"));
                }
            }
            var physiologicalSignal = new PhysiologicalSignal();

            physiologicalSignal.FromRequest(physiologicalSignalRequest);
            _coadaptService.PhysiologicalSignal.CreatePhysiologicalSignal(physiologicalSignal);
            await _coadaptService.SaveAsync();

            return(CreatedAtRoute("PhysiologicalSignalById", new { id = physiologicalSignal.Id }, physiologicalSignal));
        }