/// <summary> /// Method to add an item into the collection if the item not already exists with the predicate paste in argument. /// </summary> /// <typeparam name="T">The type of items in the collection.</typeparam> /// <param name="collection">The collection to search in and add in.</param> /// <param name="item">The object to add to the collection.</param> /// <param name="predicate">The predicate matching to check.</param> /// <returns>The updated collection.</returns> /// <exception cref="InvalidOperationException">Occurs if the predicate argument is null.</exception> public static ObservableCollection <T> AddIfNotExists <T>(this ObservableCollection <T> collection, T item, Predicate <T> predicate) { try { if (collection.Exists(predicate) == false) { collection.Add(item); } } catch (ArgumentNullException e) { ArgumentNullException ex = new ArgumentNullException($"An Exception occurs while adding object to the collection.", e); log.Error(ex.Output()); log.Error(e.Output(), e); throw ex; } catch (Exception e) { InvalidOperationException ex = new InvalidOperationException($"An Exception occurs while adding object to the collection.", e); log.Error(ex.Output()); log.Error(e.Output(), e); throw ex; } return(collection); }
/// <summary> /// Method to get an URL segment at specified index. /// </summary> /// <param name="index">The index to find.</param> /// <param name="init">The default value if the segment doesn't exists.</param> /// <param name="ucword">Uppercase first character of the string.</param> /// <returns>The segment string.</returns> private string GetSegment(int index, string init = "", bool ucword = false) { try { string [] _segments = httpListener.Request.Url.Segments; if (index > 0 && _segments.Length > index) { string segment = _segments[index].Replace("/", ""); if (ucword) { segment = segment.UCFirst(); } return(segment); } } catch (Exception e) { InvalidOperationException ex = new InvalidOperationException($"Creating Server url request segments : failed !", e); log.Fatal(ex.Output(), ex); throw ex; } return(init); }
/// <summary> /// Method to set additional Uri parameters. /// </summary> private string[] InitializeUriParameters() { try { return(httpListener .Request .Url .Segments .Skip(4) .Select(s => s.Replace("/", "")) .ToArray() ); } catch (Exception e) { InvalidOperationException ex = new InvalidOperationException($"Initializing Server url request parameters : failed !", e); log.Fatal(ex.Output(), ex); throw ex; } }
/// <summary> /// Method to update a <see cref="AlbumEntity"/> asynchronous. /// </summary> /// <param name="entity">An <see cref="AlbumEntity"/> to update.</param> /// <param name="autoDate">Auto initialize dates.</param> /// <returns>The updated <see cref="AlbumEntity"/>.</returns> public async Task <UserEntity> UpdateAsync(UserEntity entity) { if (entity == null) { return(null); } using (Db.Context) { // Try to attach entity to the database context. try { Db.Context.Attach(entity); } catch (Exception e) { InvalidOperationException i = new InvalidOperationException($"Error on database Context Attach {entity?.GetType()}", e); log.Error(i.Output()); throw; } return(await UserManager.UpdateAsync(entity)); } }
/// <summary> /// Method to update a <see cref="AlbumEntity"/> asynchronous. /// </summary> /// <param name="entity">An <see cref="AlbumEntity"/> to update.</param> /// <param name="autoDate">Auto initialize dates.</param> /// <returns>The updated <see cref="AlbumEntity"/>.</returns> public async Task <AlbumEntity> UpdateAsync(AlbumEntity entity, bool autoDate = true) { if (entity == null) { return(null); } using (Db.Context) { // Try to attach entity to the database context. try { Db.Context.Attach(entity); } catch (Exception e) { InvalidOperationException i = new InvalidOperationException($"Error on database Context Attach {entity?.GetType()}", e); log.Error(i.Output()); throw; } // Update entity. return(await AlbumManager.UpdateAsync(entity, autoDate)); } }