Esempio n. 1
0
        public async Task <IActionResult> AddOrUpdatePictureAsync(AccountSettingsViewModel viewModel, IFormFile image = null)
        {
            var id = int.Parse(HttpContext.Session.GetString("Id"));

            if (!ModelState.IsValid || image == null)
            {
                viewModel.Account = await _accountService.GetAsync(id);

                return(View("CaptainPanelEdit", viewModel));
            }

            try
            {
                if (await _imageService.ExistsForParentAsync(id) == false)
                {
                    await _imageService.AddAsync(id, image);
                }
                else
                {
                    await _imageService.UpdateAsync(id, image);
                }

                return(RedirectToAction("Profile"));
            }
            catch (Exception)
            {
                ViewBag.ShowError    = true;
                ViewBag.ErrorMessage = "Coś poszło nie tak.";

                return(RedirectToAction("EditProfile"));
            }
        }
Esempio n. 2
0
        public async Task <ImageResult> UpdateEmployeePhotoAsync(int id, ImageData imageData, IImageService imageService)
        {
            var dbEmployee = await GetEmployeeIfExistsAsync(id);

            var imageResult = await imageService.AddAsync(imageData, dbEmployee.ImagePath);

            dbEmployee.ImagePath = imageResult.NewPath;
            await _context.SaveChangesAsync();

            return(imageResult);
        }
Esempio n. 3
0
        public async Task <ActionResult <ServiceCM> > CreateService([FromForm] ServiceCM serviceModel)
        {
            //TODO: Implements AccountService.GetById(int id) does not exist, return BadRequest()
            //TODO: Implements ServiceTypeId.GetById(int id) does not exist, return BadRequest()
            //TODO: Implements GaleryId.GetById(int id) does not exist, return BadRequest()


            DateTime crtDate = DateTime.Now;
            DateTime updDate = DateTime.Now;
            string   status  = Constants.Status.ACTIVE;

            // Initialize gallery
            GalleryCM galleryCM = new GalleryCM();

            galleryCM.Name        = "Hình " + serviceModel.ServiceName.ToLower();
            galleryCM.Description = "Hình " + serviceModel.ServiceName.ToLower();;

            // Add new gallery
            Gallery gallery    = _mapper.Map <Gallery>(galleryCM);
            Gallery crtGallery = await _galleryService.AddAsync(gallery);

            await _galleryService.Save();

            ImageCM imageCM = new ImageCM();

            imageCM.Description = "Hình " + serviceModel.ServiceName;
            imageCM.GalleryId   = crtGallery.Id;
            imageCM.ImageUrl    = await _uploadFileService.UploadFile("123456798", serviceModel.File, "service", "service-detail");

            Image image = _mapper.Map <Image>(imageCM);
            await _imageService.AddAsync(image);

            Service crtService = _mapper.Map <Service>(serviceModel);

            try
            {
                crtService.CreatedDate   = crtDate;
                crtService.UpdatedDate   = updDate;
                crtService.Status        = status;
                crtService.AccountId     = serviceModel.AccountId;
                crtService.ServiceTypeId = serviceModel.ServiceTypeId;
                crtService.GalleryId     = crtGallery.Id;

                await _service.AddAsync(crtService);

                await _service.Save();
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }

            return(CreatedAtAction("GetServiceById", new { id = crtService.Id }, crtService));
        }
Esempio n. 4
0
        public async Task AddAsync_CreateNewImage_Ok()
        {
            //Arrange
            var expected = ImageApplicationStub.image_1;

            //Act
            var actual = await _imageService.AddAsync(expected);

            //Assert
            actual.Should().BeOfType(typeof(ImageApplication));
            actual.Should().NotBeNull();
            actual.Should().Equals(expected);
            actual.UserName.Should().NotBeNullOrEmpty();
        }
        public async Task <IActionResult> Add(IFormFileCollection files, [FromForm] SocialInfo socialInfo)
        {
            if (files?.Count > 0)
            {
                var imagePaths = new List <string>();

                foreach (var file in files)
                {
                    if (file.Length == 0)
                    {
                        continue;
                    }

                    var imagePath = $"{Path.GetTempFileName()}.{file.FileName}";

                    using (var fileStream = new FileStream(imagePath, FileMode.Append))
                    {
                        await file.CopyToAsync(fileStream);
                    }

                    var faces = await _imageService.DetectFaceAsync(imagePath);

                    var biggestFace = faces.OrderByDescending(x => x.Width * x.Height).FirstOrDefault();

                    if (biggestFace == null)
                    {
                        continue;
                    }

                    imagePaths.Add(biggestFace.Path);
                }

                if (imagePaths.Count > 0)
                {
                    var id = await _idService.GetAsync();

                    await _imageService.AddAsync(id, imagePaths);

                    if (socialInfo != null)
                    {
                        await _tagService.AddOrUpdateAsync(id, socialInfo);
                    }

                    return(Ok(id));
                }
            }

            return(BadRequest());
        }
Esempio n. 6
0
        public async Task <ImageResult> UpdateEmployeePhotoAsync(int id, ImageData imageData, IImageService imageService)
        {
            var dbEmployees = await _context.Employees.Where(e => e.Id == id).ToArrayAsync();

            if (dbEmployees.Length == 0)
            {
                throw new RequestedResourceNotFoundException();
            }

            var dbEmployee  = dbEmployees[0];
            var imageResult = await imageService.AddAsync(imageData, dbEmployee.ImagePath);

            dbEmployee.ImagePath = imageResult.NewImagePath;
            await _context.SaveChangesAsync();

            return(imageResult);
        }
Esempio n. 7
0
        public async Task <ActionResult <FeedBackCM> > CreateFeedback([FromForm] FeedBackCM feedbackModel)
        {
            //TODO: Implements BookingDetail.GetById(int id) does not exist, return BadRequest()
            //TODO: Implements GaleryId.GetById(int id) does not exist, return BadRequest()

            if (feedbackModel.File != null)
            {
                GalleryCM galleryCM = new GalleryCM();
                galleryCM.Name        = "Hình feedback cho dịch vụ trong đơn " + feedbackModel.BookingDetailId;
                galleryCM.Description = "Hình feedback cho dịch vụ trong đơn " + feedbackModel.BookingDetailId;

                Gallery gallery    = _mapper.Map <Gallery>(galleryCM);
                Gallery crtGallery = await _galleryService.AddAsync(gallery);

                await _galleryService.Save();

                feedbackModel.GalleryId = crtGallery.Id;

                ImageCM imageCM = new ImageCM();
                imageCM.Description = "Hình feedback trong đơn " + feedbackModel.BookingDetailId;
                imageCM.GalleryId   = crtGallery.Id;
                imageCM.ImageUrl    = await _uploadFileService.UploadFile("123456798", feedbackModel.File, "service", "service-detail");

                Image image = _mapper.Map <Image>(imageCM);
                await _imageService.AddAsync(image);
            }

            FeedBack crtFeedback = _mapper.Map <FeedBack>(feedbackModel);

            try
            {
                crtFeedback.CreateDate = DateTime.Now;

                await _service.AddAsync(crtFeedback);

                await _service.Save();
            }
            catch (Exception e)
            {
                //return StatusCode(StatusCodes.Status500InternalServerError);
                NotFound(e);
            }

            return(CreatedAtAction("GetFeedBackById", new { id = crtFeedback.Id }, crtFeedback));
        }
Esempio n. 8
0
        public async Task <ActionResult <ImageApplication> > Post(
            [FromBody] ImageApplication model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var result = await _imageService.AddAsync(model);

                return(Created("", result));
            }
            catch (Exception ex)
            {
                return(Conflict(ex.Message));

                throw;
            }
        }
Esempio n. 9
0
        public async Task <ActionResult <ImageCM> > CreateImage([FromBody] ImageCM imageModel)
        {
            /*DateTime crtDate = DateTime.Now;
            *  DateTime updDate = DateTime.Now;*/

            Image crtImage = _mapper.Map <Image>(imageModel);

            try
            {
                /*crtService.CreatedDate = crtDate;
                *  crtService.UpdatedDate = updDate;*/

                await _service.AddAsync(crtImage);

                await _service.Save();
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }

            return(CreatedAtAction("GetImageById", new { id = crtImage.Id }, crtImage));
        }
        public async Task <IActionResult> AddImage([FromBody] ImageVM image)
        {
            if (image == null)
            {
                return(BadRequest("The image model is null"));
            }
            try {
                var file       = Request.Form.Files[0];
                var folderName = Path.Combine("Resources", "ImagesStorage");
                var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);

                if (file.Length > 0)
                {
                    var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                    var fullPath = Path.Combine(pathToSave, fileName);
                    var dbPath   = Path.Combine(folderName, fileName);
                    using (var stream = new FileStream(fullPath, FileMode.Create)) {
                        file.CopyTo(stream);
                    }

                    image.CreatedAt = DateTime.Now;
                    image.Name      = fileName;
                    image.Link      = fullPath;

                    await _imageService.AddAsync(_mapper.Map <ImageDTO>(image));

                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception ex) {
                return(StatusCode(500, $"Internal server error: {ex}"));
            }
        }
        public async Task <IActionResult> AddNewImage([FromBody] ImagePostDto imagePostDto)
        {
            var imageResp = await imageService.AddAsync(imagePostDto);

            return(CreatedAtAction("GetClient", new { id = imageResp.ID }, mapper.Map <ImageResponseDto>(imageResp)));
        }
        public async Task <IActionResult> CreateNew([FromBody] ApiV1ProductCreateRequestModel model)
        {
            if (model != null && ModelState.IsValid)
            {
                logger.LogDebug(
                    $"Attempting to add new product with {model.Prices.Count} prices and {model.Translations.Count} translations.");

                var imageEntity = new ImageEntity
                {
                    Base64String = model.Image.Base64String,
                    Description  = model.Image.Description,
                    ImageType    = model.Image.ImageType
                };

                // Add image to database
                await imageService.AddAsync(imageEntity);

                var productEntity = new ProductEntity
                {
                    Artist      = model.Artist,
                    CategoryId  = model.CategoryId,
                    ImageId     = imageEntity.Id,
                    Label       = model.Label,
                    ReleaseDate = model.ReleaseDate
                };

                var productPriceEntities = model.Prices
                                           .Select(x => new ProductPriceEntity
                {
                    CurrencyId = x.CurrencyId,
                    Price      = x.Price
                })
                                           .ToList();

                var productTranslationEntities = model.Translations
                                                 .Select(x => new ProductTranslationEntity
                {
                    Description      = x.Description,
                    DescriptionShort = x.DescriptionShort,
                    LanguageId       = x.LanguageId,
                    Title            = x.Title
                })
                                                 .ToList();

                // Add product with prices and translations to database
                await productService.AddAsync(productEntity, productPriceEntities, productTranslationEntities);

                logger.LogInformation($"Successfully added new product.");

                // Get product
                var newProduct = await productService.GetByIdAsync(productEntity.Id);

                return(Ok(newProduct.ToApiV1ProductResponseModel()));
            }
            else
            {
                logger.LogWarning($"Erorr while adding product. Validation failed");

                return(BadRequest(ModelState.ToApiV1ErrorResponseModel()));
            }
        }