Example #1
0
        private async Task Send(EventBaseDto @event)
        {
            var httpClient = _httpClientFactory.CreateClient();
            var request    = new HttpRequestMessage(HttpMethod.Post, "https://localhost:44353/api/events");

            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var serJson = System.Text.Json.JsonSerializer.Serialize(@event);

            request.Content = new StringContent(serJson);
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            try
            {
                using (var response = await httpClient.SendAsync(request))
                {
                    Console.BackgroundColor = ConsoleColor.Green;
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.WriteLine("Send successful: {0}", serJson);
                    Console.ResetColor();

                    response.EnsureSuccessStatusCode();
                }
            }
            catch (HttpRequestException ex)
            {
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Send failed: {0}: {1}: {2}", ex.Message, ex.InnerException?.Message, ex.StackTrace);
                Console.ResetColor();
            }
        }
Example #2
0
        public async Task RunAsync()
        {
            var absoluteStartDate = new DateTime(2021, 4, 5, 8, 0, 0);
            var absoluteEndDate   = new DateTime(2021, 4, 9, 17, 0, 0);

            while (absoluteStartDate < absoluteEndDate)
            {
                var endDate      = absoluteStartDate.AddMinutes(30);
                var eventBaseDto = new EventBaseDto {
                    Start      = absoluteStartDate,
                    End        = endDate,
                    Text       = $"{randomText()}",
                    CalendarId = 1
                };

                absoluteStartDate = endDate;
                if (absoluteStartDate.Hour > 17)
                {
                    absoluteStartDate = new DateTime(absoluteStartDate.Year, absoluteStartDate.Month, absoluteStartDate.AddDays(1).Day, 8, 0, 0);
                }

                await Send(eventBaseDto);

                await Task.Delay(200);
            }
        }
Example #3
0
        public async Task <IActionResult> CreateEvent(EventBaseDto eventBaseDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var eventEntity = _mapper.Map <Data.Model.Event>(eventBaseDto);
            await _context.Events.AddAsync(eventEntity);

            await _context.SaveChangesAsync();

            await _publishEndpoint.Publish(_mapper.Map <Common.Contracts.EventItemCreated>(eventEntity));

            return(CreatedAtRoute(nameof(GetEventById),
                                  new { id = eventEntity.Id },
                                  _mapper.Map <EventDto>(eventEntity)));
        }
Example #4
0
        public async Task <IActionResult> UpdateEvent(int id, [FromBody] EventBaseDto eventBaseDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var eventEntity = await _context.Events.FirstOrDefaultAsync(e => e.Id == id);

            if (eventEntity == null)
            {
                return(BadRequest($"No event with id = {id} found."));
            }

            _mapper.Map(eventBaseDto, eventEntity);

            _context.Events.Attach(eventEntity);
            await _context.SaveChangesAsync();

            await _publishEndpoint.Publish(_mapper.Map <Common.Contracts.EventItemUpdated>(eventEntity));

            return(NoContent());
        }