public async Task AddAsync( IEnumerable <IFormFile> images, string imgurToken, int?eventId) { Event existingEvent = null; if (eventId != null) { existingEvent = await this.dbContext.Events.FindAsync(eventId); if (existingEvent == null) { throw new InvalidOperationException(ServiceConstants.UnexistingEvent); } } foreach (var formFile in images) { var imagePath = await ImgurApiClient.UploadImageAsync(formFile, imgurToken); var image = new Image { ImagePath = imagePath, DateOfAdded = DateTime.UtcNow, EventId = eventId }; if (existingEvent != null) { existingEvent.Images.Add(image); } else { await this.dbContext.Images.AddAsync(image); } } if (existingEvent != null) { this.dbContext.Events.Update(existingEvent); } await this.dbContext.SaveChangesAsync(); }
public async Task AddAsync( string imgurToken, string title, string content, string city, string address, IFormFile image, string imageUrl, DateTime startDate, DateTime?endDate) { this.ValidateEventData(title, content, city, address, startDate, endDate); var imageLink = string.Empty; if (image != null) { imageLink = await ImgurApiClient.UploadImageAsync(image, imgurToken); } else { imageLink = imageUrl; } var newEvent = new Event { Address = address, City = city, Content = content, EndDate = endDate.HasValue ? endDate.Value.ToUniversalTime() : endDate, StartDate = startDate.ToUniversalTime(), Title = title, BackgroundImageUrl = imageLink }; this.dbContext.Events.Add(newEvent); await this.dbContext.SaveChangesAsync(); }