示例#1
0
 public async Task InsertAdShowStatsAsync(AdShowStats adShowStats)
 {
     if (adShowStats == default)
     {
         throw new NullReferenceException($"Parameter {nameof(adShowStats)} passed into {nameof(InsertAdShowStatsAsync)} is null.");
     }
     await adsDBContext.AddAsync(adShowStats);
 }
示例#2
0
        public void UpdateAdShowStats(AdShowStats adShowStats)
        {
            if (adShowStats == default)
            {
                throw new NullReferenceException($"Parameter {nameof(adShowStats)} passed into {nameof(UpdateAdShowStats)} is null.");
            }

            adsDBContext.AdShowStats.Update(adShowStats);
        }
示例#3
0
        public async Task <AdShowStats> GetAdShowStatsAsync(int adId, DateTime date)
        {
            AdShowStats adShowStats = await adsDBContext.AdShowStats.FirstOrDefaultAsync(t => t.AdId == adId && t.Date.Date == date.Date);

            if (adShowStats == default)
            {
                throw new NullReferenceException($"{nameof(AdShowStats)} with {nameof(adId)} = {adId} and {nameof(date)} = {date.Date} not exist.");
            }

            return(adShowStats);
        }
示例#4
0
        public async Task <IActionResult> AdShowReport(string projectid, string adid)
        {
            if (!ExternalIdPassedGuidValidation(projectid))
            {
                return(StatusCode(StatusCodes.Status400BadRequest, $"Invalid {nameof(projectid)}."));
            }

            if (!ExternalIdPassedGuidValidation(adid))
            {
                return(StatusCode(StatusCodes.Status400BadRequest, $"Invalid {nameof(adid)}."));
            }

            try
            {
                if (!await ProjectExists(projectid))
                {
                    return(StatusCode(StatusCodes.Status400BadRequest, "Unknown project."));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, ex));
            }

            try
            {
                if (!await adsRepository.AdExistsAsync(adid))
                {
                    return(StatusCode(StatusCodes.Status400BadRequest, "Unknown ad."));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, ex));
            }

            Ad ad;

            try
            {
                ad = await adsRepository.GetAdAsync(adid);
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }

            DateTime date = DateTime.UtcNow.Date;

            try
            {
                bool adShowStatsExists = await adsRepository.AdShowStatsExistsAsync(ad.Id, date);

                if (!adShowStatsExists)
                {
                    AdShowStats adShowStats = new AdShowStats()
                    {
                        AdId   = ad.Id,
                        Date   = date,
                        Number = 1
                    };
                    await adsRepository.InsertAdShowStatsAsync(adShowStats);
                }
                else
                {
                    AdShowStats adShowStats = await adsRepository.GetAdShowStatsAsync(ad.Id, date);

                    adShowStats.Number++;
                }

                await adsRepository.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }

            return(Ok());
        }