Ejemplo n.º 1
0
        public async void PublishItemTest()
        {
            var item = new ItemDataDTO()
            {
                Category    = "Other",
                Name        = "",
                Description = "nonempty",
                InitLicit   = 10,
                Expiration  = DateTime.Now.AddDays(1),
                Image       = null,
            };

            int countBefore = await portalContext.Items.CountAsync();

            var result = await service.InsertItem(item);

            int countAfter = await portalContext.Items.CountAsync();

            Assert.Null(result.Id);
            Assert.Equal(countBefore, countAfter);

            item.Name = "nonempty";

            result = await service.InsertItem(item);

            countAfter = await portalContext.Items.CountAsync();

            Assert.NotNull(result.Id);
            Assert.Equal(countBefore + 1, countAfter);
        }
Ejemplo n.º 2
0
        public async Task <InsertionResultDTO> InsertItem(ItemDataDTO itemData)
        {
            if (String.IsNullOrWhiteSpace(itemData.Name))
            {
                return(Error("Name must not be empty"));
            }
            if (String.IsNullOrWhiteSpace(itemData.Description))
            {
                return(Error("Description must not be empty"));
            }
            if (itemData.Expiration <= DateTime.Now)
            {
                return(Error("Expiration date must be in the future."));
            }
            if (itemData.InitLicit <= 0)
            {
                return(Error("Initial licit must be greater than zero."));
            }
            var category = await context.Categories
                           .Where(cat => cat.Name == itemData.Category)
                           .FirstOrDefaultAsync();

            if (category is null)
            {
                return(Error($"Category '{itemData.Category}' does not exist."));
            }
            var publisher =
                await userManager.GetUserAsync(httpContext.User)
                ?? throw new Exception("A publisher must be signed in");

            Item item = new Item()
            {
                Category    = category,
                Description = itemData.Description,
                Expiration  = itemData.Expiration,
                Image       = itemData.Image,
                InitLicit   = itemData.InitLicit,
                Name        = itemData.Name,
                PublishDate = DateTime.Now,
                Publisher   = publisher
            };
            await context.Items.AddAsync(item);

            context.SaveChanges();
            return(new InsertionResultDTO()
            {
                Id = item.Id
            });
        }
Ejemplo n.º 3
0
        public async Task <InsertionResultDTO> InsertItemAsync(ItemDataDTO itemDTO)
        {
            try
            {
                using (HttpResponseMessage response = await _client.PostAsJsonAsync(API_ITEMS, itemDTO))
                { // az értékeket azonnal JSON formátumra alakítjuk
                    var result = await response.Content.ReadAsAsync <InsertionResultDTO>();

                    return(result);
                }
            }
            catch (Exception ex)
            {
                throw new PersistenceUnavailableException(ex);
            }
        }
Ejemplo n.º 4
0
 public Task <InsertionResultDTO> InsertItemAsync(ItemDataDTO article)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 5
0
        public async Task <InsertionResultDTO> Post([FromBody] ItemDataDTO itemData)
        {
            var result = await service.InsertItem(itemData);

            return(result);
        }