Esempio n. 1
0
        public async Task <IActionResult> AddAds(VIPAdsForCreationDto entity)
        {
            var user = await _userRepo.GetUserByUserClaims(HttpContext.User);

            if (user == null)
            {
                return(Unauthorized("User is Unauthorized"));
            }

            var vipAd = new VIPAd()
            {
                Name      = entity.Name,
                ImageUrl  = entity.ImageUrl,
                DateAdded = entity.DateAdded
            };

            if (await _vipRepo.GetById(vipAd.Id) != null)
            {
                return(BadRequest("This Ad is exist."));
            }

            if (await _vipRepo.Add(vipAd))
            {
                return(Ok(vipAd));
            }
            return(BadRequest("Error happen when add Ads"));
        }
        public static async Task SeedVIPAds(StoreContext context)
        {
            if (!context.VIPAds.Any())
            {
                var vipAdsData = await File.ReadAllTextAsync("../Services/Seeds/Data/VIPAds.json");

                var vipAds = JsonSerializer.Deserialize <List <VIPAd> >(vipAdsData);
                foreach (var vipAd in vipAds)
                {
                    var vipAdForAdd = new VIPAd
                    {
                        Name      = vipAd.Name.ToLower(),
                        ImageUrl  = vipAd.ImageUrl,
                        DateAdded = DateTime.UtcNow
                    };

                    await context.AddAsync(vipAdForAdd);

                    await context.SaveChangesAsync();
                }
            }
        }