Advertisement
Guest User

eventcontroller

a guest
Feb 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection.Metadata.Ecma335;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.EntityFrameworkCore;
  9.  
  10. namespace WebApplicationTestPrax.Controllers
  11. {
  12.     [Route("api/[controller]")]
  13.     [ApiController]
  14.     public class EventsController : ControllerBase
  15.     {
  16.         private readonly PartyContext _context;
  17.         private readonly ParticipantContext _participantContext;
  18.         public EventsController(PartyContext context, ParticipantContext participantContext)
  19.         {
  20.             _context = context;
  21.             _participantContext = participantContext;
  22.             if (_context.Parties.Count() == 0)
  23.             {
  24.                 // Create a new TodoItem if collection is empty,
  25.                 // which means you can't delete all TodoItems.
  26.                 _context.Parties.Add(new Party { Name = "Esimene üritus", Id="1",Description = "Kirjeldus üks"});
  27.                 _context.SaveChanges();
  28.             }
  29.         }
  30.  
  31.         [HttpGet("{id}/{subcontroller}")]
  32.         public ActionResult<IEnumerable<Participant>> GetParticipants(string id, string subcontroller)
  33.         {
  34.             var allParticipants = _participantContext.Participants.Where(x => x.EventId == id).ToList();
  35.             return allParticipants;
  36.         }
  37.         [HttpGet("{id}/{subcontroller}/{participantId}")]
  38.         public async Task<ActionResult<Participant>> GetParticipant(string id, string subcontroller,string participantId)
  39.         {
  40.             var participants = _participantContext.Participants.Where(x => x.EventId == id &&  x.Id == participantId).ToList();
  41.             var participant = participants[0];
  42.             return participant;
  43.         }
  44.         [HttpGet("{id}")]
  45.         public async Task<ActionResult<Party>> GetParty(string id)
  46.         {
  47.             var party = await _context.Parties.FindAsync(id);
  48.  
  49.             if (party == null)
  50.             {
  51.                 return NotFound();
  52.             }
  53.  
  54.             return party;
  55.         }
  56.         [HttpGet]
  57.         public async Task<ActionResult<IEnumerable<Party>>> GetParties()
  58.         {
  59.             return await _context.Parties.ToListAsync();
  60.         }
  61.        
  62.         // POST api/values
  63.         [HttpPost]
  64.         public async Task<ActionResult<Party>> PostParty([FromBody]Party party)
  65.         {
  66.             if (!ModelState.IsValid)
  67.             {
  68.                 return BadRequest(ModelState);
  69.             }
  70.  
  71.             _context.Parties.Add(party);
  72.             await _context.SaveChangesAsync();
  73.             //siin salvestame andmebaasi
  74.             return party;
  75.         }
  76.         [HttpPost("{id}/{subcontroller}")]
  77.         public ActionResult<Party> PostParticipant([FromBody]Participant participant, string id)
  78.         {
  79.             if (!ModelState.IsValid)
  80.             {
  81.                 return BadRequest(ModelState);
  82.             }
  83.  
  84.             participant.EventId = id;
  85.             _participantContext.Participants.Add(participant);
  86.             _participantContext.SaveChanges();
  87.             var party = _context.Parties.Find(id);
  88.             _context.SaveChanges();
  89.             //siin salvestame andmebaasi
  90.             return party;
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement