~jpgleeson/caint

8f81ab9de13d8e75419e3de408f3cb3db5b088d0 — jack gleeson a month ago 11b01e1 master
Remove the admin stuff from the API. This is handled by the dashboard
5 files changed, 1 insertions(+), 246 deletions(-)

M Controllers/CommentsController.cs
D wwwroot/dashboard.html
D wwwroot/js/caint-admin.js
M wwwroot/js/caint.js
M wwwroot/thread.html
M Controllers/CommentsController.cs => Controllers/CommentsController.cs +0 -52
@@ 30,41 30,6 @@ namespace caint.Controllers
            return await _context.comments.Select(x => ItemToDTO(x)).ToListAsync();
        }

        [HttpGet("admin")]
        public async Task<ActionResult<IEnumerable<Comment>>> GetCommentsAdmin()
        {
            var commentList = await _context.comments.ToListAsync();

            return commentList;
        }

        [HttpGet("admin/{id}")]
        public async Task<ActionResult<IEnumerable<Comment>>> GetCommentsAdmin(long id)
        {
            var commentList = await _context.comments.Where(x => x.threadId == id).ToListAsync();

            Console.WriteLine(commentList.Count);

            return commentList;
        }

        [HttpPost("admin/approve/{id}")]
        public async Task<IActionResult> PutComment(long id)
        {
            var comment = await _context.comments.FindAsync(id);

            if (comment == null)
            {
                return NotFound();
            }

            comment.approved = true;

            await _context.SaveChangesAsync();

            return NoContent();
        }

        // GET: api/Comments/5
        [HttpGet("{id}")]
        public async Task<ActionResult<CommentDTO>> GetComment(long id)


@@ 178,23 143,6 @@ namespace caint.Controllers
            return NotFound();
        }


        // DELETE: api/Comments/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteComment(long id)
        {
            var comment = await _context.comments.FindAsync(id);
            if (comment == null)
            {
                return NotFound();
            }

            _context.comments.Remove(comment);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool CommentExists(long id)
        {
            return _context.comments.Any(e => e.id == id);

D wwwroot/dashboard.html => wwwroot/dashboard.html +0 -47
@@ 1,47 0,0 @@
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Caint admin</title>
        <link rel="stylesheet" href="css/caint-admin.css" />
    </head>
    <body>
        <div class="full-width-Header">
            <div class="centreLeft">
                <div class ="headerTitle">caint</div>
            </div>
        </div>

        <div class="bodyContainer">

        <div id="editForm">
            <h3>Edit</h3>
            <form action="javascript:void(0);" onsubmit="updateItem()">
                <input type="hidden" id="edit-id">
                <input type="checkbox" id="edit-isComplete">
                <input type="text" id="edit-name">
                <input type="submit" value="Save">
                <a onclick="closeInput()" aria-label="Close">&#10006;</a>
            </form>
        </div>

        <p id="counter"></p>

        <table>
            <tr>
                <th>Approved?</th>
                <th>Thread</th>
                <th>Name</th>
                <th class="commentBoxTable">Comment</th>
                <th></th>
                <th></th>
                <th></th>
            </tr>
            <tbody id="comments"></tbody>
        </table>

    </div>

        <script src="js/caint-admin.js" asp-append-version="true">getItems();</script>
    </body>
    </html>

D wwwroot/js/caint-admin.js => wwwroot/js/caint-admin.js +0 -138
@@ 1,138 0,0 @@
const uri = 'api/Comments';
const threadUri = 'api/Threads';

const threadHost = "TENANTNAMEHERE";
const threadPath = document.location.pathname;

getItems();

function getItems() {
  console.log("fetching items");
  fetch(uri + "/admin")
    .then(response => response.json())
    .then(data => _displayItems(data))
    .catch(error => console.error('Unable to get comments.', error));
}

function getThread(thread) {
  fetch(uri + "/thread/" + thread)
    .then(response => response.json())
    .then(data => _displayItems(data))
    .catch(error => console.error('Unable to get comments.', error));
}

function deleteItem(id) {
  fetch(`${uri}/${id}`, {
    method: 'DELETE'
  })
  .then(() => getItems())
  .catch(error => console.error('Unable to delete item.', error));
}

function approveItem(id) {
    fetch(`${uri}/admin/approve/${id}`, {
        method: 'POST'
      })
      .then(() => getItems())
      .catch(error => console.error('Unable to approve comments.', error));
}

function displayEditForm(id) {
  const item = todos.find(item => item.id === id);
  
  document.getElementById('edit-name').value = item.name;
  document.getElementById('edit-id').value = item.id;
  document.getElementById('edit-isComplete').checked = item.isComplete;
  document.getElementById('editForm').style.display = 'block';
}

function updateItem() {
  const itemId = document.getElementById('edit-id').value;
  const item = {
    id: parseInt(itemId, 10),
    isComplete: document.getElementById('edit-isComplete').checked,
    name: document.getElementById('edit-name').value.trim()
  };

  fetch(`${uri}/${itemId}`, {
    method: 'PUT',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(item)
  })
  .then(() => getItems())
  .catch(error => console.error('Unable to update item.', error));

  closeInput();

  return false;
}

function closeInput() {
  document.getElementById('editForm').style.display = 'none';
}

function _displayCount(itemCount) {
  const name = (itemCount === 1) ? 'comment' : 'comments';

  document.getElementById('counter').innerText = `${itemCount} ${name}`;
}

function _displayItems(data) {
  const tBody = document.getElementById('comments');
  tBody.innerHTML = '';

  _displayCount(data.length);

  const button = document.createElement('button');

  data.forEach(item => {
    let isCompleteCheckbox = document.createElement('input');
    isCompleteCheckbox.type = 'checkbox';
    isCompleteCheckbox.disabled = true;
    isCompleteCheckbox.checked = item.approved;

    let approveButton = button.cloneNode(false);
    approveButton.innerText = 'Approve';
    approveButton.setAttribute('onclick', `approveItem(${item.id})`);

    let editButton = button.cloneNode(false);
    editButton.innerText = 'Edit';
    editButton.setAttribute('onclick', `displayEditForm(${item.id})`);

    let deleteButton = button.cloneNode(false);
    deleteButton.innerText = 'Delete';
    deleteButton.setAttribute('onclick', `deleteItem(${item.id})`);

    let tr = tBody.insertRow();
    
    let td1 = tr.insertCell(0);
    td1.appendChild(isCompleteCheckbox);

    let td2 = tr.insertCell(1);
    let threadIdNode = document.createTextNode(item.threadId);
    td2.setAttribute('onclick', 'getThread(${item.threadID})');
    td2.appendChild(threadIdNode);

    let td3 = tr.insertCell(2);
    let commenterNode = document.createTextNode(item.name);
    td3.appendChild(commenterNode);

    let td4 = tr.insertCell(3);
    let bodyNode = document.createTextNode(item.body);
    td4.appendChild(bodyNode);

    let td5 = tr.insertCell(4);
    td5.appendChild(approveButton);

    let td6 = tr.insertCell(5);
    td6.appendChild(editButton);

    let td7 = tr.insertCell(6);
    td7.appendChild(deleteButton);
  });

  comments = data;
}
\ No newline at end of file

M wwwroot/js/caint.js => wwwroot/js/caint.js +0 -8
@@ 96,14 96,6 @@ function deleteItem(id) {
  .catch(error => console.error('Unable to delete item.', error));
}

function approveItem(id) {
    fetch(`${uri}/admin/approve/${id}`, {
        method: 'POST'
      })
      .then(() => getItems())
      .catch(error => console.error('Unable to approve comments.', error));
}

function closeInput() {
  document.getElementById('editForm').style.display = 'none';
}

M wwwroot/thread.html => wwwroot/thread.html +1 -1
@@ 2,7 2,7 @@
<html>
<head>
    <meta charset="UTF-8">
    <title>Caint admin</title>
    <title>Sample Caint integration</title>
    <link rel="stylesheet" href="css/caint.css" />
</head>
<body>