Notes
Notes - notes.io |
using Microsoft.AspNetCore.Identity.Data;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using WebApplication10.models;
namespace WebApplication10.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EvenshtController : ControllerBase
{
private static readonly List<User> Users = new List<User>();
private static readonly List<Event> Events = new List<Event>();
private static readonly Dictionary<string, List<string>> EventDetails = new Dictionary<string, List<string>>();
private static readonly Dictionary<string, List<string>> UserEvents = new Dictionary<string, List<string>>();
// POST api/event/register
[HttpPost("register")]
public IActionResult Register(User user)
{
if (Users.Any(u => u.Username == user.Username))
{
return BadRequest("user already exist");
}
Users.Add(user);
return Ok("registration sucessfull ");
}
// POST api/event/login
[HttpPost("login")]
public IActionResult Login([FromBody] User user)
{
if (ModelState.IsValid)
{
if (Users.Any(u => u.Username == user.Username && u.Password == user.Password))
{
return Ok(new { message = "Login successful", username = user.Username });
}
return BadRequest("Invalid credentials.");
}
return BadRequest("Invalid model state");
}
// GET api/event/upcoming
[HttpGet("upcoming")]
public IActionResult GetUpcomingEvents()
{
if (Events.Count == 0)
{
return Ok("no events yet");
}
return Ok(Events);
}
[HttpPost("create")]
public ActionResult CreateEvent([FromBody] Event ev)
{
if (ev == null)
{
return BadRequest("Invalid data");
}
// Check if an event with the same location and date-time already exists
if (Events.Any(e => e.Location == ev.Location && e.Date == ev.Date))
{
return BadRequest("An event with the same location and date-time already exists");
}
Events.Add(ev);
return Ok($"Event {ev.Name} created successfully!");
}
[HttpPut("edit/{eventName}")]
public ActionResult EditEvent(string eventName, [FromBody] Event updatedEvent)
{
var existingEvent = Events.FirstOrDefault(e => e.Name == eventName);
if (existingEvent == null)
{
return NotFound($"Event with name {eventName} not found");
}
// Check if the updated location and date-time conflict with other events
if (Events.Any(e => e.Name != eventName && e.Location == updatedEvent.Location && e.Date == updatedEvent.Date && e.Description == updatedEvent.Description))
{
return BadRequest("Another event with the same location and date-time already exists");
}
// Update the existing event
existingEvent.Name = updatedEvent.Name;
existingEvent.Location = updatedEvent.Location;
existingEvent.Date = updatedEvent.Date;
existingEvent.Description = updatedEvent.Description;
return Ok($"Event {eventName} updated successfully!");
}
[HttpDelete("delete/{eventName}")]
public ActionResult DeleteEvent(string eventName)
{
var eventToRemove = Events.FirstOrDefault(e => e.Name == eventName);
if (eventToRemove == null)
{
return NotFound($"Event with name {eventName} not found");
}
Events.Remove(eventToRemove);
return Ok($"Event {eventName} deleted successfully!");
}
//join
[HttpPost("join")]
public ActionResult JoinEvent([FromBody] JoinRequest joinRequest)
{
string eventName = joinRequest.Eventname;
string username = joinRequest.Username;
// Check if the event exists
Event eventDetails = Events.FirstOrDefault(e => e.Name == eventName);
if (eventDetails == null)
{
return NotFound($"Event '{eventName}' does not exist.");
}
if (UserEvents.ContainsKey(username))
{
// Ensure the user's event list is initialized
UserEvents[username] = UserEvents[username] ?? new List<string>();
// Check if the user has already joined the event
if (UserEvents[username].Contains($"Participant: {username}, Event: {eventName}"))
{
return Conflict($"User '{username}' has already joined the event '{eventName}'.");
}
// Check if the user has already booked an event on the same date
if (UserEvents[username].Any(e => Events.Any(ev => ev.Date.Date == eventDetails.Date.Date)))
{
return Conflict($"User '{username}' has already booked an event on the same date.");
}
// Add the user to the event
UserEvents[username].Add($"Participant: {username}, Event: {eventName}");
// Add more event information as needed
return Ok($"Successfully joined event: {eventName}");
}
else
{
return NotFound($"User '{username}' not found.");
}
}
[HttpGet("track/{username}")]
public IActionResult TrackEvents(string username)
{
if (UserEvents.ContainsKey(username))
{
List<string> trackedEvents = UserEvents[username];
if (trackedEvents.Count == 0)
{
return Ok("No events tracked yet.");
}
List<Event> userEventsDetails = new List<Event>();
foreach (string trackedEvent in trackedEvents)
{
// Extract event name from trackedEvent
string eventName = trackedEvent.Split(',')[1].Trim().Split(':')[1].Trim();
// Find the event in Events list
Event eventDetails = Events.FirstOrDefault(e => e.Name == eventName);
if (eventDetails != null)
{
userEventsDetails.Add(eventDetails);
}
}
// Sort events by date
userEventsDetails = userEventsDetails.OrderBy(e => e.Date).ToList();
// Construct detailed event information
List<string> detailedEventInfo = userEventsDetails
.Select(e => $"Event: {e.Name}, Location: {e.Location}, Date: {e.Date}, Description: {e.Description}")
.ToList();
return Ok(detailedEventInfo);
}
return Ok("No events tracked yet.");
}
///manage the event
[HttpGet("manage/{username}")]
public ActionResult Manage(string username)
{
if (UserEvents.ContainsKey(username))
{
List<string> trackedEvents = UserEvents[username];
List<string> detailedEventInfo = new List<string>();
foreach (string trackedEvent in trackedEvents)
{
// Extract event name from trackedEvent string
string eventName = trackedEvent.Split(',')[1].Trim().Split()[1].Trim();
// Find the event in Events list created by the specified user
Event eventDetails = Events.FirstOrDefault(e => e.Name == eventName && e.Creator == username);
if (eventDetails != null)
{
// Construct detailed event information
string eventInfo = $"Event: {eventDetails.Name}, Location: {eventDetails.Location}, Date: {eventDetails.Date}";
detailedEventInfo.Add(eventInfo);
}
}
return Ok(detailedEventInfo);
}
else
{
return NotFound($"User '{username}' not found.");
}
}
[HttpGet("get-appointments/{username}")]
public ActionResult GetAppointments(string username)
{
if (UserEvents.ContainsKey(username))
{
List<string> joinedEvents = UserEvents[username];
List<object> joinedEventInfo = new List<object>();
foreach (string joinedEvent in joinedEvents)
{
// Extract event name from joinedEvent string
string eventName = joinedEvent.Split(',')[1].Trim().Split(':')[1].Trim(); // Fix the index
// Find the event in Events list
Event eventDetails = Events.FirstOrDefault(e => e.Name == eventName);
if (eventDetails != null)
{
// Construct joined event information
var eventInfo = new
{
Name = eventDetails.Name,
DateTime = eventDetails.Date,
Location = eventDetails.Location
};
joinedEventInfo.Add(eventInfo);
}
}
return Ok(joinedEventInfo);
}
else
{
return NotFound($"User '{username}' not found.");
}
}
}
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team