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.Reflection.Metadata.Ecma335;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.EntityFrameworkCore;
- namespace WebApplicationTestPrax.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class EventsController : ControllerBase
- {
- private readonly PartyContext _context;
- private readonly ParticipantContext _participantContext;
- public EventsController(PartyContext context, ParticipantContext participantContext)
- {
- _context = context;
- _participantContext = participantContext;
- if (_context.Parties.Count() == 0)
- {
- // Create a new TodoItem if collection is empty,
- // which means you can't delete all TodoItems.
- _context.Parties.Add(new Party { Name = "Esimene üritus", Id="1",Description = "Kirjeldus üks"});
- _context.SaveChanges();
- }
- }
- [HttpGet("{id}/{subcontroller}")]
- public ActionResult<IEnumerable<Participant>> GetParticipants(string id, string subcontroller)
- {
- var allParticipants = _participantContext.Participants.Where(x => x.EventId == id).ToList();
- return allParticipants;
- }
- [HttpGet("{id}/{subcontroller}/{participantId}")]
- public async Task<ActionResult<Participant>> GetParticipant(string id, string subcontroller,string participantId)
- {
- var participants = _participantContext.Participants.Where(x => x.EventId == id && x.Id == participantId).ToList();
- var participant = participants[0];
- return participant;
- }
- [HttpGet("{id}")]
- public async Task<ActionResult<Party>> GetParty(string id)
- {
- var party = await _context.Parties.FindAsync(id);
- if (party == null)
- {
- return NotFound();
- }
- return party;
- }
- [HttpGet]
- public async Task<ActionResult<IEnumerable<Party>>> GetParties()
- {
- return await _context.Parties.ToListAsync();
- }
- // POST api/values
- [HttpPost]
- public async Task<ActionResult<Party>> PostParty([FromBody]Party party)
- {
- if (!ModelState.IsValid)
- {
- return BadRequest(ModelState);
- }
- _context.Parties.Add(party);
- await _context.SaveChangesAsync();
- //siin salvestame andmebaasi
- return party;
- }
- [HttpPost("{id}/{subcontroller}")]
- public ActionResult<Party> PostParticipant([FromBody]Participant participant, string id)
- {
- if (!ModelState.IsValid)
- {
- return BadRequest(ModelState);
- }
- participant.EventId = id;
- _participantContext.Participants.Add(participant);
- _participantContext.SaveChanges();
- var party = _context.Parties.Find(id);
- _context.SaveChanges();
- //siin salvestame andmebaasi
- return party;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement