public DamageReportCompleteOutputDto ModifyDamageReport(string token, int id, ModifyDamageReportDto modifiedDamage)
        {
            _credentialsVerifier.VerifyCredentialsForModifyingDamageReports(token);

            DamageReport damageReport = _damageReportsRepository.Get(id);

            DamageStatus oldStatus = damageReport.Status;

            _objectMapper.Map(modifiedDamage, damageReport);

            DamageStatus newStatus = damageReport.Status;

            CheckIfDamageIsModifiable(damageReport);

            if (modifiedDamage.Status == DamageStatusDto.Repairing)
            {
                damageReport.CrewMemberThatRepairedTheDamage = _userRepository.Get(modifiedDamage.Crew.Id) as Crew;
            }

            CurrentUnitOfWork.SaveChanges();

            NotifyStatusChange(oldStatus, newStatus);

            return(_objectMapper.Map <DamageReportCompleteOutputDto>(damageReport));
        }
        public DamageReportCompleteOutputDto Get(int id, string token)
        {
            _credentialsVerifier.VerifyCredentialsForQueryingDamageReports(token);

            User user = UserUtillities.GetExecutingUserIfLoggedIn(token, _sessionsRepository);

            DamageReport damageReport = _damageReportsRepository
                                        .GetAllIncluding(dr => dr.MediaResources)
                                        .Where(user.DamageReportsICanQuery())
                                        .FirstOrDefault(dr => dr.Id == id);

            return(_objectMapper.Map <DamageReportCompleteOutputDto>(damageReport));
        }
 private static void CheckIfDamageIsModifiable(DamageReport damageReport)
 {
     if (damageReport.Status == DamageStatus.Repaired)
     {
         throw new ModelValidationException(
                   new List <ValidationErrorDto>
         {
             new ValidationErrorDto
             {
                 Field = "Status", Message = "Cannot repair damage in this endpoint."
             }
         });
     }
 }
        private void BindMediaResources(DamageReport damage)
        {
            int[] mediaResourceIds = damage.MediaResources.Select(mr => mr.Id).ToArray();

            foreach (int mediaResourceId in mediaResourceIds)
            {
                Logic.Entities.Media dbMedia = _mediaRepository.Get(mediaResourceId);

                if (dbMedia == null)
                {
                    throw new InvalidRequestException();
                }

                damage.MediaResources.RemoveAll(mr => mr.Id == mediaResourceId);

                damage.MediaResources.Add(dbMedia);
            }
        }
        public DamageReportCompleteOutputDto RepairDamage(string token, DamageReportRepairDto damage)
        {
            _credentialsVerifier.VerifyCredentialsForRepairingDamageReports(token);

            DamageReport dbDamageReport = _damageReportsRepository
                                          .GetAllIncluding(d => d.MediaResources).FirstOrDefault(d => d.Id == damage.Id);

            _objectMapper.Map(damage, dbDamageReport);

            BindRepairedMediaResources(dbDamageReport);

            dbDamageReport.Status = DamageStatus.Repaired;

            CurrentUnitOfWork.SaveChanges();

            _damageReportManager.SendDamageRepairedNotification(GetDevicesOf(token));

            return(_objectMapper.Map <DamageReportCompleteOutputDto>(dbDamageReport));
        }
        public DamageReportOutputDto ReportDamage(DamageReportInputDto damageDto, string token)
        {
            _credentialsVerifier.VerifyCredentialsForDamageReporting(token);

            DamageReport damage = _objectMapper.Map <DamageReport>(damageDto);

            if (!damage.IsValid())
            {
                throw new ModelValidationException(_objectMapper.Map <List <ValidationErrorDto> >(damage.GetValidationErrors()));
            }

            BindMediaResources(damage);

            damage.User = UserUtillities.GetExecutingUserIfLoggedIn(token, _sessionsRepository);

            _damageReportsRepository.Insert(damage);

            CurrentUnitOfWork.SaveChanges();

            return(_objectMapper.Map <DamageReportOutputDto>(damage));
        }