public async Task <IActionResult> Post(
            [FromServices] IFileSaver fileSaver,
            [FromServices] IConfiguration configuration,
            [FromForm] ProductAddCommand command,
            ApiVersion version
            )
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (command.Photo != null && command.Photo.Length > 0)
            {
                command.PhotoName = await fileSaver.SaveAsync(command.Photo, configuration["Settings:ProductPhotoDirectory"]);
            }

            var id = await _mediator.Send(command);

            await _unitOfWork.CommitAsync();

            return(CreatedAtAction(nameof(Get), new { id, Version = $"{version}" }, new ProductDto {
                Id = id
            }));
        }
示例#2
0
        public async Task <IActionResult> Post([FromForm] IEnumerable <IFormFile> pictures)
        {
            ProductAddCommand command = null;

            try
            {
                command = JsonConvert.DeserializeObject <ProductAddCommand>(Request.Form["Product"].ToString());

                command.Pictures = await UploadProductFiles(pictures, null);

                return(Ok
                       (
                           await service.AddAsync(command, GetPathTemplate(Request))
                       ));
            }
            catch (DuplicateWaitObjectException ex)
            {
                DeleteProductFiles(command.Pictures?.Select(x => new FileInfoDto(GetPathTemplate(Request), x.Key, x.Value)));
                return(Conflict(ex.Message));
            }
            catch (HttpRequestException ex)
            {
                return(StatusCode(StatusCodes.Status503ServiceUnavailable, ex.Message));
                //return BadRequest(ex.Message);
            }
            catch (Exception ex)
            {
                DeleteProductFiles(command.Pictures?.Select(x => new FileInfoDto(GetPathTemplate(Request), x.Key, x.Value)));
                System.Diagnostics.Debug.Print(ex.ToString());
                return(BadRequest(Error));
            }
        }
示例#3
0
        public async Task <IActionResult> Put(string id, [FromForm] IEnumerable <IFormFile> pictures)
        {
            ProductAddCommand command = null;

            try
            {
                var oldProduct = await service.GetProductAsync
                                 (
                    id,
                    GetPathTemplate(Request)
                                 );

                if (oldProduct == null)
                {
                    throw new KeyNotFoundException($"Product {id} not found !");
                }

                command = JsonConvert.DeserializeObject <ProductAddCommand>(Request.Form["Product"].ToString());

                command.Pictures = await UploadProductFiles(pictures, oldProduct.Pictures);

                await service.EditAsync
                (
                    id,
                    command,
                    GetPathTemplate(Request)
                );

                return(NoContent());
            }
            catch (KeyNotFoundException ex)
            {
                return(NotFound(ex.Message));
            }
            catch (DuplicateWaitObjectException ex)
            {
                return(Conflict(ex.Message));
            }
            catch (HttpRequestException ex)
            {
                return(StatusCode(StatusCodes.Status503ServiceUnavailable, ex.Message));
                //return BadRequest(ex.Message);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.ToString());
                return(BadRequest(Error));
            }
        }
示例#4
0
        public async Task Create_Duplicate_Product_Returns_Error()
        {
            // Arrange
            var command = new ProductAddCommand
            {
                Code         = InitialProductList.Products.First().Code.Value,
                Name         = "Some Product",
                Price        = 9.99m,
                CurrencyCode = CurrencyCode.Euro
            };
            var content = new StringContent(JsonSerializer.Serialize(command), Encoding.UTF8, "application/json");

            // Act
            var response = await _client.PostAsync(ApiUrl, content);

            // Assert
            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
        }
        public async Task EditAsync(string id, ProductAddCommand command, string path)
        {
            try
            {
                var oldProduct1 = await GetProductAsync(id, path);

                if (oldProduct1 == null)
                {
                    throw new KeyNotFoundException($"{nameof(Product)} {id} not found");
                }

                var oldProduct2 = await GetProductByReferenceAsync(command.Reference, path);

                if (oldProduct2 != null && oldProduct2.Id != id)
                {
                    throw new DuplicateWaitObjectException($"{nameof(Product)} {command.Reference} already exists !");
                }

                await FirebaseClient
                .Child(Table)
                .Child(id)
                .PutAsync
                (
                    JsonConvert.SerializeObject
                    (
                        new Product
                        (
                            command.Reference,
                            command.Name,
                            command.Description,
                            command.Price,
                            command.Currency,
                            command.Pictures.Select
                            (
                                x =>
                                new FileInfo
                                (
                                    x.Key,
                                    x.Value

                                )
                            ).ToArray(),
                            command.CategoryId,
                            command.OwnerId,
                            oldProduct1.CreatedAt,
                            command.Status
                        )
                    )
                );
            }
            catch (Firebase.Database.FirebaseException ex)
            {
                if (ex.InnerException?.InnerException?.GetType() == typeof(SocketException))
                {
                    throw new HttpRequestException("Cannot join the server. Please check your internet connexion.");
                }
            }
            catch (KeyNotFoundException ex)
            {
                throw ex;
            }
            catch (DuplicateWaitObjectException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <ProductDto> AddAsync(ProductAddCommand command, string path)
        {
            try
            {
                if (await GetProductByReferenceAsync(command.Reference, path) != null)
                {
                    throw new DuplicateWaitObjectException($"{nameof(command.Reference)} {command.Reference} already exists !");
                }

                var result = await FirebaseClient
                             .Child(Table)
                             .PostAsync
                             (
                    JsonConvert.SerializeObject
                    (
                        new Product
                        (
                            command.Reference,
                            command.Name,
                            command.Description,
                            command.Price,
                            command.Currency,
                            command.Pictures.Select
                            (
                                x =>
                                new FileInfo
                                (
                                    x.Key,
                                    x.Value

                                )
                            ).ToArray(),
                            command.CategoryId,
                            command.OwnerId,
                            command.CreatedAt,
                            command.Status
                        )
                    )
                             );

                return(GetProductDto
                       (
                           result.Key,
                           new Product
                           (
                               command.Reference,
                               command.Name,
                               command.Description,
                               command.Price,
                               command.Currency,
                               command.Pictures.Select
                               (
                                   x =>
                                   new FileInfo
                                   (
                                       x.Key,
                                       x.Value

                                   )
                               ).ToArray(),
                               command.CategoryId,
                               command.OwnerId,
                               command.CreatedAt,
                               command.Status
                           ),
                           path,
                           await categoryService.GetCategoryAsync(command.CategoryId),
                           await userService.GetUserAsync(command.OwnerId)
                       ));
            }
            catch (Firebase.Database.FirebaseException ex)
            {
                if (ex.InnerException?.InnerException?.GetType() == typeof(SocketException))
                {
                    throw new HttpRequestException("Cannot join the server. Please check your internet connexion.");
                }
                throw ex;
            }
            catch (DuplicateWaitObjectException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }