public async Task <IActionResult> SetAdmin([FromBody] Dto.EditRelookeuse editRelookeuse)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            Relookeuse relookeuse = await _context.Relookeuse.FirstOrDefaultAsync(rel => rel.UserId == int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value));

            if (relookeuse == null)
            {
                return(NotFound());
            }

            relookeuse.IsPro       = editRelookeuse.IsPro;
            relookeuse.Description = editRelookeuse.Description;

            _context.Entry(relookeuse).OriginalValues["RowVersion"] = editRelookeuse.RowVersion;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(Conflict());
            }

            return(Ok(new Dto.RelookeuseRowVersion()
            {
                RowVersion = relookeuse.RowVersion
            }));
        }
        public async Task <IActionResult> DeletePicture([FromBody] Dto.RelookeuseRowVersion relRowVersion)
        {
            Relookeuse relookeuse = await _context.Relookeuse.FirstOrDefaultAsync(r => r.UserId == int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value));

            string oldPicture = relookeuse.Picture;

            relookeuse.Picture = null;

            _context.Entry(relookeuse).OriginalValues["RowVersion"] = relRowVersion.RowVersion;

            try
            {
                await _context.SaveChangesAsync();

                if (oldPicture != null)
                {
                    await _blobContainerClient.GetBlobClient(oldPicture.Split("/").Last()).DeleteIfExistsAsync();
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                return(Conflict(ConflictErrorType.RelookeuseNewlyModified));
            }

            return(Ok(new Dto.RelookeuseRowVersion {
                RowVersion = relookeuse.RowVersion
            }));
        }
        public async Task <IActionResult> NewInscription([FromBody] Dto.RelookeuseInscription relookeuseInscription)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            int userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (await _context.Relookeuse.FirstOrDefaultAsync(rel => rel.UserId == userId) != null)
            {
                return(Conflict());
            }

            Relookeuse relookeuse = _mapper.Map <Relookeuse>(relookeuseInscription);

            relookeuse.UserId = userId;

            await _context.AddAsync(relookeuse);

            await _context.SaveChangesAsync();

            relookeuse = await _context.Relookeuse.Include(rel => rel.User).FirstOrDefaultAsync(rel => rel.UserId == userId);

            Dto.NewRelookeuse newRelookeuse = _mapper.Map <Dto.NewRelookeuse>(relookeuse);
            newRelookeuse.NewToken = await Utils.CreateTokenFor(relookeuse.User, _jwtOptions);

            return(Created("api/relookeuse/" + userId, newRelookeuse));
        }
        public async Task <IActionResult> GetMyDetails()
        {
            Relookeuse relookeuse = await _context.Relookeuse.Where(rel => rel.UserId == int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)).Include(relookeuse => relookeuse.User).Include(relookeuse => relookeuse.Appointment).OrderByDescending(relookeuse => relookeuse.Appointment.Sum(appointment => appointment.Mark) / relookeuse.Appointment.Count(appointment => appointment.Mark != null)).FirstOrDefaultAsync();

            if (relookeuse == null)
            {
                return(NotFound(int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)));
            }

            return(Ok(_mapper.Map <Relookeuse, Dto.Relookeuse>(relookeuse)));
        }
        public async Task <IActionResult> GetDetail(int id)
        {
            if (id < 1)
            {
                return(BadRequest());
            }

            Relookeuse relookeuse = await _context.Relookeuse.Where(rel => rel.UserId == id).Include(rel => rel.User).Include(rel => rel.Appointment).Include(rel => rel.PortfolioPicture).Include(rel => rel.Tarif).Include(rel => rel.Availability).FirstOrDefaultAsync();

            if (relookeuse == null)
            {
                return(NotFound(id));
            }

            relookeuse.Availability = relookeuse.Availability.OrderBy(availability => availability.StartTime).ToList();

            return(Ok(_mapper.Map <Relookeuse, Dto.DetailedRelookeuse>(relookeuse)));
        }
        public async Task <IActionResult> ReplacePicture([FromForm] Dto.Picture picture)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Relookeuse relookeuse = await _context.Relookeuse.FirstOrDefaultAsync(r => r.UserId == int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value));

            string oldPicture = relookeuse.Picture;

            BlobClient blobClient = _blobContainerClient.GetBlobClient(Guid.NewGuid() + Path.GetExtension(picture.File.FileName));

            relookeuse.Picture = blobClient.Uri.ToString();

            _context.Entry(relookeuse).OriginalValues["RowVersion"] = picture.RowVersion;

            try
            {
                await _context.SaveChangesAsync();

                if (oldPicture != null)
                {
                    await _blobContainerClient.GetBlobClient(oldPicture.Split("/").Last()).DeleteIfExistsAsync();
                }
                await using var stream = picture.File.OpenReadStream();
                await blobClient.UploadAsync(stream);
            }
            catch (DbUpdateConcurrencyException)
            {
                return(Conflict(ConflictErrorType.RelookeuseNewlyModified));
            }

            return(Ok(new Dto.ProfilPicture {
                Url = blobClient.Uri.ToString(), RowVersion = relookeuse.RowVersion
            }));
        }