/// <summary> /// Adds a new spoken message to a sermon series /// </summary> /// <param name="request"></param> /// <returns></returns> public async Task <SystemResponse <SermonSeries> > AddMessageToSermonSeries(string SeriesId, AddMessagesToSeriesRequest request) { var validRequest = AddMessagesToSeriesRequest.ValidateRequest(request); if (validRequest.HasErrors) { return(new SystemResponse <SermonSeries>(true, validRequest.ErrorMessage)); } if (string.IsNullOrEmpty(SeriesId)) { return(new SystemResponse <SermonSeries>(true, string.Format(SystemMessages.NullProperty, "SeriesId"))); } // if we can't find it then the Id is invalid var getSermonSeriesResponse = await _sermonsRepository.GetSermonSeriesForId(SeriesId); if (getSermonSeriesResponse.HasErrors) { return(new SystemResponse <SermonSeries>(true, getSermonSeriesResponse.ErrorMessage)); } var series = getSermonSeriesResponse.Result; // add the sermon message to the response object and re-update the Mongo doc var currentMessages = series.Messages.ToList(); var messageList = new List <SermonMessage>(); foreach (var message in request.MessagesToAdd) { messageList.Add(new SermonMessage { AudioFileSize = message.AudioFileSize, AudioDuration = message.AudioDuration, // sanitise the message dates and get rid of the times Date = message.Date.Value.Date.ToUniversalTime().Date, MessageId = Guid.NewGuid().ToString(), AudioUrl = message.AudioUrl, PassageRef = message.PassageRef, Speaker = message.Speaker, Title = message.Title, VideoUrl = message.VideoUrl }); } currentMessages.AddRange(messageList); // readd the messages back to the object, This is important (see SO for Deep Copy vs shallow copy) series.Messages = currentMessages; // find and replace the one with the updated object var updateResponse = await _sermonsRepository.UpdateSermonSeries(series); if (updateResponse.HasErrors) { return(new SystemResponse <SermonSeries>(true, updateResponse.ErrorMessage)); } // Save data in cache. _cache.Set(string.Format(CacheKeys.GetSermonSeries, updateResponse.Result.Id), updateResponse.Result, PersistentCacheEntryOptions); return(new SystemResponse <SermonSeries>(updateResponse.Result, "Success!")); }