public static void FromRequest(this OuraSleep ouraSleep, OuraSleepRequest request)
 {
     ouraSleep.ParticipantId     = request.ParticipantId;
     ouraSleep.SummaryDate       = request.SummaryDate;
     ouraSleep.PeriodId          = request.PeriodId;
     ouraSleep.TimezoneOffset    = request.TimezoneOffset;
     ouraSleep.BedtimeStart      = request.BedtimeStart;
     ouraSleep.BedtimeEnd        = request.BedtimeEnd;
     ouraSleep.Score             = request.Score;
     ouraSleep.ScoreTotal        = request.ScoreTotal;
     ouraSleep.ScoreDisturbances = request.ScoreDisturbances;
     ouraSleep.ScoreAlignment    = request.ScoreAlignment;
     ouraSleep.ScoreDeep         = request.ScoreDeep;
     ouraSleep.ScoreEfficiency   = request.ScoreEfficiency;
     ouraSleep.ScoreLatency      = request.ScoreLatency;
     ouraSleep.ScoreRem          = request.ScoreRem;
     ouraSleep.Total             = request.Total;
     ouraSleep.Duration          = request.Duration;
     ouraSleep.Awake             = request.Awake;
     ouraSleep.Light             = request.Light;
     ouraSleep.Rem              = request.Rem;
     ouraSleep.Deep             = request.Deep;
     ouraSleep.OnsetLatency     = request.OnsetLatency;
     ouraSleep.Restless         = request.Restless;
     ouraSleep.Efficiency       = request.Efficiency;
     ouraSleep.MidpointTime     = request.MidpointTime;
     ouraSleep.HrLowest         = request.HrLowest;
     ouraSleep.HrAverage        = request.HrAverage;
     ouraSleep.RmsSd            = request.RmsSd;
     ouraSleep.BreathAverage    = request.BreathAverage;
     ouraSleep.TemperatureDelta = request.TemperatureDelta;
     ouraSleep.Hypnogram5Min    = request.Hypnogram5Min;
     ouraSleep.Hr5Min           = string.Join(",", request.Hr5Min);
     ouraSleep.RmsSd5Min        = string.Join(",", request.RmsSd5Min);
 }
        public async Task <IActionResult> CreateOuraSleep([FromBody] OuraSleepRequest ouraSleepRequest)
        {
            if (ouraSleepRequest == null)
            {
                _logger.LogError("CreateOuraSleep: OuraSleepRequest object sent from client is null.");
                return(BadRequest("OuraSleepRequest object is null"));
            }
            if (!ModelState.IsValid)
            {
                _logger.LogError("CreateOuraSleep: Invalid OuraSleepRequest object sent from client.");
                return(BadRequest("Invalid OuraSleepRequest 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(ouraSleepRequest.ParticipantId, userId))
                {
                    return(BadRequest("A sub-administrator can create only oura sleeps of a participant of own organization"));
                }
            }
            else if (role == Role.SupervisorRole)
            {
                if (!await ParticipantInStudiesOfUserIdAsync(ouraSleepRequest.ParticipantId, userId))
                {
                    return(BadRequest("A supervisor can create only oura sleeps of a participant of own studies"));
                }
            }
            else if (role == Role.ParticipantRole)
            {
                if (!await ParticipantSameAsUserIdAsync(ouraSleepRequest.ParticipantId, userId))
                {
                    return(BadRequest("A participant can create only own oura sleeps"));
                }
            }
            else if (role == Role.TherapistRole)
            {
                var participant = await _coadaptService.Participant.GetParticipantByIdAsync(ouraSleepRequest.ParticipantId);

                if (!await ParticipantMonitoredByTherapistOfUserIdAsync(participant, userId))
                {
                    return(BadRequest("A therapist can create only oura sleeps of monitored participants"));
                }
            }
            var ouraSleep = new OuraSleep();

            ouraSleep.FromRequest(ouraSleepRequest);
            _coadaptService.OuraSleep.CreateOuraSleep(ouraSleep);
            await _coadaptService.SaveAsync();

            return(CreatedAtRoute("OuraSleepById", new { id = ouraSleep.Id }, ouraSleep));
        }