~jpgleeson/caint

ref: 33ef43ddbf22d0c7f0b741a50d225e656bb471e6 caint/Controllers/CommentsController.cs -rw-r--r-- 5.9 KiB
33ef43dd — jpgleeson FIX: Add ownerId to new comments. 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Cors;
using Microsoft.EntityFrameworkCore;
using caint.Data;
using caint.Models;

namespace caint.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CommentsController : ControllerBase
    {
        private readonly caintDBContext _context;

        public CommentsController(caintDBContext context)
        {
            _context = context;
        }

        // GET: api/Comments
        [HttpGet]
        public async Task<ActionResult<IEnumerable<CommentDTO>>> Getcomments()
        {
            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)
        {
            var comment = await _context.comments.FindAsync(id);

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

            return ItemToDTO(comment);
        }

        //[EnableCors]
        [HttpGet("thread/{id}")]
        public async Task<ActionResult<IEnumerable<Comment>>> GetCommentThread(long id)
        {
            var comment = await _context.comments.ToListAsync();
            List<Comment> returnList = new List<Comment>();

            foreach (Comment singleComment in comment)
            {
                if (singleComment.threadId == id && singleComment.approved)
                {
                    returnList.Add(singleComment);
                }
            }

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

            return returnList;
        }

        // PUT: api/Comments/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public async Task<IActionResult> PutComment(long id, CommentDTO CommentDTO)
        {
            if (id != CommentDTO.id)
            {
                return BadRequest();
            }

            _context.Entry(CommentDTO).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CommentExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/Comments
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        //[EnableCors]
        [HttpPost]
        public async Task<ActionResult<CommentDTO>> PostComment(CommentDTO commentDTO)
        {
            var comment = new Comment
            {
                approved = false,
                name = commentDTO.name,
                body = commentDTO.body,
                threadId = commentDTO.threadId
            };

            var thread = await _context.threads.FindAsync(comment.threadId);

            string tenantName = thread.hostname;

            var tenant = _context.tenants.Where(x => x.tenantName == tenantName).FirstOrDefault();
            if (tenant == null)
            {

            }
            else
            {
                comment.ownerId = tenant.ownerId;
                
                if (tenant.active)
                {
                    //It might be worthwhile putting the acceptance in here so that we catch comments
                    //for users who's accounts have lapsed, but they just can't approve them from the dashboard.
                }
                _context.comments.Add(comment);
                await _context.SaveChangesAsync();

                return CreatedAtAction(nameof(GetComment), new { id = comment.id }, ItemToDTO(comment));
            }
            //Not sure if this is the best response here, as it returns a 404 to the AJAX request, but it's sufficient for now.
            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);
        }

        private static CommentDTO ItemToDTO(Comment comment) =>
        new CommentDTO 
        {
            id = comment.id,
            name = comment.name,
            body = comment.body,
            threadId = comment.threadId
        };
    }
}