Exemple #1
0
        public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto)
        {
            var sender = await _repo.getUser(userId);

            if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            messageForCreationDto.SenderId = userId;
            var recipient = await _repo.getUser(messageForCreationDto.RecipientId);

            if (recipient == null)
            {
                return(BadRequest("Could not find user"));
            }

            var message = _mapper.Map <Message>(messageForCreationDto);

            _repo.Add(message);

            if (await _repo.SaveAll())
            {
                var messageToReturn = _mapper.Map <MessageToReturnDto>(message);
                return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn));
            }

            throw new Exception("Creating the message failed on save");
        }
        public async Task <IActionResult> GetUser(int id)
        {
            var user = await _repo.getUser(id);

            var userToReturn = _mapper.Map <UserForDetailedDto>(user);

            return(Ok(userToReturn));
        }
        public async Task <IActionResult> AddDocumentForUser(int userId,
                                                             [FromForm] DocumentForCreationDto docForCreationDto)
        {
            // [FromForm]PhotoForCreationDto = PhotoForCreationDto)

            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                int DocumentUploadeeId = userId;
                int DocumentUploaderId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
                var checkIfUploaderSelectedUploadee = await _repo.GetSelect(DocumentUploaderId, DocumentUploadeeId);

                var checkIfUploadeeSelectedUploader = await _repo.GetSelect(DocumentUploadeeId, DocumentUploaderId);

                if (checkIfUploadeeSelectedUploader != null && checkIfUploaderSelectedUploadee == null)
                {
                    return(BadRequest("You cannot upload photos until you accept user's request"));
                }
                else if (checkIfUploadeeSelectedUploader == null && checkIfUploaderSelectedUploadee == null)
                {
                    return(Unauthorized());
                }
            }

            var userFromRepo = await _repo.getUser(userId);

            var file = docForCreationDto.File;

            var imageUploadResult = new ImageUploadResult();
            var videoUploadResult = new VideoUploadResult();

            // var uploadResult = new RawUploadResult();


            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    if (file.ContentType.Contains("image"))
                    {
                        var uploadParams = new ImageUploadParams()
                        {
                            File           = new FileDescription(file.Name, stream),
                            Transformation = new Transformation()
                                             .Width(500).Height(500).Crop("fill").Gravity("face")
                        };
                        imageUploadResult          = _cloudinary.Upload(uploadParams);
                        docForCreationDto.Url      = imageUploadResult.Uri.ToString();
                        docForCreationDto.PublicId = imageUploadResult.PublicId;
                    }
                    if (file.ContentType.Contains("video"))
                    {
                        var uploadParams = new VideoUploadParams()
                        {
                            File            = new FileDescription(file.Name, stream),
                            EagerTransforms = new List <Transformation>()
                            {
                                new Transformation().Width(300).Height(300)
                                .Crop("pad").AudioCodec("none"),
                                new Transformation().Width(160).Height(100)
                                .Crop("crop").Gravity("south").AudioCodec("none")
                            }
                        };
                        videoUploadResult          = _cloudinary.Upload(uploadParams);
                        docForCreationDto.Url      = videoUploadResult.Uri.ToString();
                        docForCreationDto.PublicId = videoUploadResult.PublicId;
                    }

                    // var uploadParams = new RawUploadParams() {
                    //     File = new FileDescription(file.Name, stream),
                    //     // Transformation = new Transformation()
                    //     //   .Width(500).Height(500).Crop("fill").Gravity("face")
                    // };

                    // uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            // docForCreationDto.Url = uploadResult.Uri.ToString();
            // docForCreationDto.PublicId = uploadResult.PublicId;
            docForCreationDto.Type = file.ContentType;

            var document = _mapper.Map <Document>(docForCreationDto);

            // if (!userFromRepo.Documents.Any(u => u.IsMain))
            //     photo.IsMain = true;

            userFromRepo.Documents.Add(document);

            //var photoToReturn = _mapper.Map<PhotosForReturnDto>(photo);

            if (await _repo.SaveAll())
            {
                var docToReturn = _mapper.Map <DocumentForReturnDto>(document);
                return(CreatedAtRoute("GetDocument", new { id = document.id }, docToReturn));
            }

            return(BadRequest("Could not add the photo"));
        }
        public async Task <IActionResult> CreateAppointment(int userId, AppointmentForCreationDto apptForCreation)
        {
            var apptCreator = await _repo.getUser(userId);

            if (apptCreator.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var apptRecipient = (User)null;

            if (apptCreator.UserRole.RoleId.Equals(1))
            {
                apptForCreation.PatientId = userId;

                apptRecipient = await _repo.getUser(apptForCreation.DoctorId);

                if (apptRecipient == null)
                {
                    return(BadRequest("Could not find Doctor"));
                }
            }
            else if (apptCreator.UserRole.RoleId.Equals(3))
            {
                apptForCreation.DoctorId = userId;

                apptRecipient = await _repo.getUser(apptForCreation.PatientId);

                if (apptRecipient == null)
                {
                    return(BadRequest("Could not find Patient"));
                }
            }
            //messageForCreationDto.SenderId = userId;
            // var apptRecipient = await _repo.getUser(apptForCreation.RecipientId);
            // if (recipient == null) {
            //     return BadRequest("Could not find user");
            // }
            var appointment = _mapper.Map <Appointment>(apptForCreation);

            appointment.Patient = await _repo.getUser(appointment.PatientId);

            appointment.Doctor = await _repo.getUser(appointment.DoctorId);


            // CHECK IF APPOINTMENT ALREADY EXISTS ON THIS DATE
            UserParams userParams = new UserParams();

            userParams.UserId = appointment.PatientId;
            var patientApptsGet = await _repo.GetAppointmentsForUser(userParams);

            for (int i = 0; i < patientApptsGet.Count; i++)
            {
                var appt = _mapper.Map <AppointmentToReturnDto>(appointment);
                if (patientApptsGet[i].StartDate.CompareTo(appt.StartDate) == 0)
                {
                    return(BadRequest("This date is unavailable."));
                }
            }

            userParams.UserId = appointment.DoctorId;
            var doctorApptsGet = await _repo.GetAppointmentsForUser(userParams);

            for (int i = 0; i < doctorApptsGet.Count; i++)
            {
                var appt = _mapper.Map <AppointmentToReturnDto>(appointment);
                if (doctorApptsGet[i].StartDate.CompareTo(appt.StartDate) == 0)
                {
                    return(BadRequest("This date is unavailable."));
                }
            }



            _repo.Add(appointment);

            if (await _repo.SaveAll())
            {
                var apptToReturn = _mapper.Map <AppointmentToReturnDto>(appointment);
                return(CreatedAtRoute("GetAppointment", new { id = appointment.Id }, apptToReturn)); //, messageToReturn);
            }
            throw new Exception("Creating the appointment failed on save");
        }