/// <summary> /// Basicly transformthe board view model to the Board entity object. /// </summary> /// <param name="boardViewModel">The <see cref="BoardViewModel"/> object we will transform to a domain object.</param> /// <returns>The transformed <see cref="Board"/> object.</returns> public static Board BoardViewModelToBoard(BoardViewModel boardViewModel) { var board = new Board() { Id = boardViewModel.Id, BoardName = boardViewModel.BoardName, }; return board; }
/// <summary> /// Transforms the Board entity object to a Board View Model object /// </summary> /// <param name="board">The entity <see cref="Board"/> object we will transform to a view model.</param> /// <returns><see cref="BoardViewModel"/> object.</returns> public static BoardViewModel BoardToBoardViewModel(Board board) { var boardViewModel = new BoardViewModel() { BoardName = board.BoardName, Id = board.Id, UserId = board.Owner == null? Guid.Empty : board.Owner.Id }; return boardViewModel; }
/// <summary> /// Stores the board object. If the board is already present it gets updated. If there is no such board the board will be created. /// </summary> /// <param name="board">The board object that will be stored.</param> /// <param name="userId"> </param> /// <returns>The stored board object</returns> /// <remarks>If the board object is newly created the board that will be returned will contain the unique id of the created board in the database.</remarks> public Board SaveOrUpdateBoard(Board board, Guid userId) { try { // Calling the board repo Save or Update methos var storedBoard = _boardRepository.SaveOrUpdateBoard(board, userId); // return the object we got back from the repo which can be null ( failiure ) return storedBoard; } catch (Exception ex) { // If repo call goes wrong return null, instead of throwing an exception return null; } }
/// <summary> /// Stores the passed in board information. If a board exsists with the passed in board id it will get updated. If a board does not exsist /// with the passed in board id or the id is Guid.Empty a new board will be created. /// </summary> /// <param name="board">The board object that will be stored in the database, either created or updated information</param> /// <param name="userId"> </param> /// <returns>The stored <see cref="Board"/> object</returns> public Board SaveOrUpdateBoard(Board board, Guid userId) { // Save a new board if the board id is an empty guid, which means // it hasnt been saved before if(board.Id == Guid.Empty) { board.Id = Guid.NewGuid(); board.Owner = _noteContext.Users.FirstOrDefault(x => x.Id == userId); board = _noteContext.Boards.Add(board); } else { // if it has an id it has been added before so we attach it and set it in modified state board.Owner = _noteContext.Users.FirstOrDefault(x => x.Id == userId); _noteContext.Boards.Attach(board); _noteContext.Entry(board).State = EntityState.Modified; } // Either way we must save the changes to the context _noteContext.SaveChanges(); return board; }