Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.EntityFrameworkCore;
- using BottleBitAPI.Models;
- using BottleBitAPI.Repositories;
- using Opc.UaFx.Client;
- namespace BottleBitAPI.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class CommandsController : ControllerBase
- {
- private readonly CommandContext _context;
- /*
- Creates a context (database connection) everytime our controller
- is called - This way we don't have to have an open connection all the time
- */
- public CommandsController(CommandContext context)
- {
- _context = context;
- }
- // GET: api/Commands
- [HttpGet]
- public async Task<ActionResult<IEnumerable<Command>>> GetCommands()
- {
- return await _context.Commands.ToListAsync();
- }
- // GET: api/Commands/opc - Starts the DEMO from Blackboard with a GET request
- [HttpGet("opc")]
- public string TestOPC()
- {
- TestConnection tc = new TestConnection();
- tc.TestingClass();
- return "Return string here....";
- }
- // GET: api/Commands/5
- [HttpGet("{id}")]
- public async Task<ActionResult<Command>> GetCommand(long id)
- {
- var command = await _context.Commands.FindAsync(id);
- if (command == null)
- {
- return NotFound();
- }
- return command;
- }
- // PUT: api/Commands/5
- // To protect from overposting attacks, please enable the specific properties you want to bind to, for
- // more details see https://aka.ms/RazorPagesCRUD.
- [HttpPut("{id}")]
- public async Task<IActionResult> PutCommand(long id, Command command)
- {
- if (id != command.Id)
- {
- return BadRequest();
- }
- _context.Entry(command).State = EntityState.Modified;
- try
- {
- await _context.SaveChangesAsync();
- }
- catch (DbUpdateConcurrencyException)
- {
- if (!CommandExists(id))
- {
- return NotFound();
- }
- else
- {
- throw;
- }
- }
- return NoContent();
- }
- // POST: api/Commands
- // To protect from overposting attacks, please enable the specific properties you want to bind to, for
- // more details see https://aka.ms/RazorPagesCRUD.
- [HttpPost]
- public async Task<ActionResult<Command>> PostCommand(Command command)
- {
- _context.Commands.Add(command);
- await _context.SaveChangesAsync();
- return CreatedAtAction("GetCommand", new { id = command.Id }, command);
- }
- // DELETE: api/Commands/5
- [HttpDelete("{id}")]
- public async Task<ActionResult<Command>> DeleteCommand(long id)
- {
- var command = await _context.Commands.FindAsync(id);
- if (command == null)
- {
- return NotFound();
- }
- _context.Commands.Remove(command);
- await _context.SaveChangesAsync();
- return command;
- }
- private bool CommandExists(long id)
- {
- return _context.Commands.Any(e => e.Id == id);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement