// CONSTRUCTORS /// <summary> /// Constructor with parameters. /// </summary> /// <param name="canvas">Current canvas.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when canvas is null. /// </exception> public AddPentagon(Models.Canvas canvas) { if (canvas == null) { throw new System.ArgumentNullException("Canvas is null."); } this.canvas = canvas; }
/// <summary> /// The method for getting canvas by id from database. /// </summary> /// <param name="data"> Data about canvas, data like id of owner and canvas. </param> /// <returns> String with data about received canvas. </returns> public string GetCanvasById(CanvasByIdData data) { string ownerId = data.ownerId, canvasId = data.canvasId; /// <summary> /// Try to find canvas by canvas and user id. /// </summary> try { Models.Canvas canvas = Collection.Find(item => item._id == canvasId && item.ownerId == ownerId).First(); if (canvas == null) { return($"{{\"error\": \"There is no canvas with same id\"}}"); } ; string blocksInfo = "["; // Counter to determine the element during an array traversal. int index = 0; // Index last element in array. int countItems = canvas.data.Count - 1; foreach (Models.CanvasItemInData item in canvas.data) { blocksInfo += "{" + $"\"position\": [{item.position[0]}, {item.position[1]}, {item.position[2]}, {item.position[3]}], " + $"\"title\": \"{item.title}\", " + $"\"content\": \"{item.content}\", " + $"\"description\": \"{item.description}\" " + $"}}{(countItems != index ? ',' : ' ')}"; index++; } blocksInfo += "]"; string canvasData = "{" + $"\"id\": \"{canvas._id}\", " + $"\"ownerId\": \"{canvas.ownerId}\", " + $"\"title\": \"{canvas.title}\", " + $"\"type\": \"{canvas.type}\", " + $"\"date\": \"{canvas.date}\", " + $"\"rows\": \"{canvas.rows}\", " + $"\"columns\": \"{canvas.columns}\", " + $"\"data\": {blocksInfo}" + "}"; return(canvasData); } catch { return($"{{\"error\": \"Something_went_wrong\"}}"); } }
/// <summary> /// The method to get the requested template from database. /// </summary> /// <param name="type"> Type of the needed template </param> /// <returns> variable of the canvas template model </returns> public Models.Canvas GetCanvasTemplateByType(string type) { try { Models.Canvas canvas = Collection.Find(item => item.type == type).First(); return(canvas); } catch { throw new Exception($"{{\"error\": \"Something_went_wrong\"}}"); } }
/// <summary> /// The method to insert a new canvas template to database CanvasTemplate collection. /// </summary> /// <param name="data"> Infromation about a new canvas template like type, rows, etc. </param> /// <returns> Json with id of created canvas template. </returns> public string CreateCanvasTemplate(CreateCanvasTemplateModel data) { string type = data.type; // Rows count in a grid layout int rows = data.rows; // Columns count in a grid layout int columns = data.columns; List <Models.CanvasItemInData> canvasDataList = data.data; if (type == null) { return("{\"error\": \"Type_not_found\"}"); } if (rows == null) { return("{\"error\": \"Rows_not_found\"}"); } if (columns == null) { return("{\"error\": \"Columns_not_found\"}"); } if (canvasDataList == null) { return("{\"error\": \"Data_not_found\"}"); } Models.Canvas canvasTemplate = new Models.Canvas() { ownerId = null, title = null, type = data.type, date = DateTime.Now, rows = data.rows, columns = data.columns, data = canvasDataList }; /// <summary> /// Try to insert the created canvas model to MongoDB database in CanvasTemplate collection. /// </summary> try { Collection.InsertOne(canvasTemplate); return($"{{\"id\": \"{canvasTemplate._id}\"}}"); } catch { return("{\"error\": \"Something_went_wrong\"}"); } }
// CONSTRUCTORS /// <summary> /// Constructor with 3 parameters /// </summary> /// <param name="baseCanvas">Vertex in which will be added vertex</param> /// <param name="target">Added vertex</param> /// <param name="workCommandManger">program command manager</param> /// <exception cref="System.NullReferenceException">Pentagon, command manager or vertex doesn't exist!</exception> public AddVertex(Models.Canvas baseCanvas, Models.Vertex target, Models.UndoRedoManager workCommandManger) { if (baseCanvas == null) { throw new System.NullReferenceException("Canvas doesn't exist!"); } if (target == null) { throw new System.NullReferenceException("Vertex doesn't exist!"); } if (workCommandManger == null) { throw new System.NullReferenceException("Command manager doesn't exist!"); } this.baseCanvas = baseCanvas; this.target = target; this.workCommandManger = workCommandManger; }
// CONSTRUCTORS /// <summary> /// Constructor with parameters. /// </summary> /// <param name="canvas">Current canvas.</param> /// <param name="vertex">Current vertex.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when canvas is null. /// </exception> /// <exception cref="System.ArgumentNullException"> /// Thrown when vertex is null. /// </exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// Thrown when vertex hasn`t been found in canvas. /// </exception> public RemoveVertex(Models.Canvas canvas, Models.Vertex vertex) { if (canvas == null) { throw new System.ArgumentNullException("Canvas is null."); } if (vertex == null) { throw new System.ArgumentNullException("Vertex is null."); } this.canvas = canvas; this.vertex = vertex; index = canvas.IndexOf(vertex); if (index == -1) { throw new System.ArgumentOutOfRangeException("Vertex hasn`t been found."); } }
/// <summary> /// The method for creating a new canvas. /// </summary> /// <param name="data"> Information about canvas like title, type, etc. </param> /// <returns> Id of the new created canvas. </returns> public string CreateCanvas(CreateCanvasData data) { string ownerId = data.ownerId, title = data.title, type = data.type; // Get a template of canvas of the requested type from database (canvas template collection). Models.Canvas canvas = CanvasTemplateRepository.GetCanvasTemplateByType(type); canvas._id = ""; canvas.ownerId = ownerId; canvas.title = title; /// <summary> /// Try to insert a new canvas to database. /// </summary> try { Collection.InsertOne(canvas); return($"{{\"id\": \"{canvas._id}\"}}"); } catch { return($"{{\"error\": \"Something went wrong\"}}"); } }