/// <summary> /// Add a new product to the database /// </summary> /// <param name="item">product to add</param> /// <returns>null if the operattion fail, or the product inserted</returns> public Product AddProduct(Product item) { try { item.Id = ObjectId.GenerateNewId().ToString(); products.InsertOne(item); return item; } catch (Exception ex) { Debug.WriteLine(ex + " : " + ex.Message); return null; } }
/// <summary> /// Update the product with the given id /// </summary> /// <param name="id">id of the product</param> /// <param name="item">New values to uptdate</param> /// <returns>true if the operation success false otherwise</returns> public bool UpdateProduct(string id, Product item) { try { var filter = Builders<Product>.Filter.Eq("_id", id); var update = Builders<Product>.Update.Set("Name", item.Name).Set("Price", item.Price).Set("Category", item.Category); var result = products.UpdateOne(filter, update); return result.ModifiedCount == 1; } catch (Exception ex) { Debug.WriteLine(ex + " : " + ex.Message); return false; } }