Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.EntityFrameworkCore;
  8. using BottleBitAPI.Models;
  9. using BottleBitAPI.Repositories;
  10. using Opc.UaFx.Client;
  11.  
  12. namespace BottleBitAPI.Controllers
  13. {
  14.     [Route("api/[controller]")]
  15.     [ApiController]
  16.     public class CommandsController : ControllerBase
  17.     {
  18.         private readonly CommandContext _context;
  19.  
  20.         /*
  21.             Creates a context (database connection) everytime our controller
  22.             is called - This way we don't have to have an open connection all the time
  23.         */
  24.  
  25.         public CommandsController(CommandContext context)
  26.         {
  27.             _context = context;
  28.         }
  29.  
  30.         // GET: api/Commands
  31.         [HttpGet]
  32.         public async Task<ActionResult<IEnumerable<Command>>> GetCommands()
  33.         {
  34.             return await _context.Commands.ToListAsync();
  35.         }
  36.  
  37.         // GET: api/Commands/opc - Starts the DEMO from Blackboard with a GET request
  38.         [HttpGet("opc")]
  39.         public string TestOPC()
  40.         {
  41.            
  42.             TestConnection tc = new TestConnection();
  43.             tc.TestingClass();
  44.  
  45.             return "Return string here....";
  46.  
  47.         }
  48.  
  49.         // GET: api/Commands/5
  50.         [HttpGet("{id}")]
  51.         public async Task<ActionResult<Command>> GetCommand(long id)
  52.         {
  53.             var command = await _context.Commands.FindAsync(id);
  54.  
  55.             if (command == null)
  56.             {
  57.                 return NotFound();
  58.             }
  59.  
  60.             return command;
  61.         }
  62.  
  63.         // PUT: api/Commands/5
  64.         // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  65.         // more details see https://aka.ms/RazorPagesCRUD.
  66.         [HttpPut("{id}")]
  67.         public async Task<IActionResult> PutCommand(long id, Command command)
  68.         {
  69.             if (id != command.Id)
  70.             {
  71.                 return BadRequest();
  72.             }
  73.  
  74.             _context.Entry(command).State = EntityState.Modified;
  75.  
  76.             try
  77.             {
  78.                 await _context.SaveChangesAsync();
  79.             }
  80.             catch (DbUpdateConcurrencyException)
  81.             {
  82.                 if (!CommandExists(id))
  83.                 {
  84.                     return NotFound();
  85.                 }
  86.                 else
  87.                 {
  88.                     throw;
  89.                 }
  90.             }
  91.  
  92.             return NoContent();
  93.         }
  94.  
  95.         // POST: api/Commands
  96.         // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  97.         // more details see https://aka.ms/RazorPagesCRUD.
  98.         [HttpPost]
  99.         public async Task<ActionResult<Command>> PostCommand(Command command)
  100.         {
  101.             _context.Commands.Add(command);
  102.             await _context.SaveChangesAsync();
  103.  
  104.             return CreatedAtAction("GetCommand", new { id = command.Id }, command);
  105.         }
  106.  
  107.         // DELETE: api/Commands/5
  108.         [HttpDelete("{id}")]
  109.         public async Task<ActionResult<Command>> DeleteCommand(long id)
  110.         {
  111.             var command = await _context.Commands.FindAsync(id);
  112.             if (command == null)
  113.             {
  114.                 return NotFound();
  115.             }
  116.  
  117.             _context.Commands.Remove(command);
  118.             await _context.SaveChangesAsync();
  119.  
  120.             return command;
  121.         }
  122.  
  123.         private bool CommandExists(long id)
  124.         {
  125.             return _context.Commands.Any(e => e.Id == id);
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement