NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using WebApplication5;

[ApiController]
[Route("api/[controller]")]
public class AdminController : ControllerBase
{
public readonly AdminRepository _adminRepository;
public readonly EmployeeRepository _employeeRepository;

public AdminController(AdminRepository adminRepository, EmployeeRepository employeeRepository)
{
_adminRepository = adminRepository;
_employeeRepository = employeeRepository;
}

[HttpPost("login")]
public IActionResult Login([FromBody] Admin admin)
{
if (admin == null || string.IsNullOrWhiteSpace(admin.Username) || string.IsNullOrWhiteSpace(admin.Password))
{
return BadRequest("Invalid username or password");
}

if (_adminRepository.ValidateCredentials(admin.Username, admin.Password))
{
return Ok("Login successful");
}
else
{
return Unauthorized("Invalid username or password");
}
}

[HttpGet("employees")]
public IActionResult GetAllEmployees()
{
var employees = _employeeRepository.GetAllEmployees();
return Ok(employees);
}

[HttpGet("employees/{id}")]
public IActionResult GetEmployeeById(int id)
{
var employee = _employeeRepository.GetEmployeeById(id);
if (employee != null)
{
return Ok(employee);
}
else
{
return NotFound("Employee not found");
}
}

[HttpPost("employees")]
public IActionResult AddEmployee([FromBody] Employee employee)
{
if (employee == null)
{
return BadRequest("Employee object cannot be null");
}

_employeeRepository.AddEmployee(employee);
return CreatedAtAction(nameof(GetEmployeeById), new { id = employee.Id }, employee);
}

[HttpPut("employees/{id}")]
public IActionResult UpdateEmployee(int id, [FromBody] Employee employee)
{
if (employee == null || id != employee.Id)
{
return BadRequest("Invalid employee data");
}

_employeeRepository.UpdateEmployee(employee);
return Ok("Employee updated successfully");
}

[HttpDelete("employees/{id}")]
public IActionResult DeleteEmployee(int id)
{
_employeeRepository.DeleteEmployee(id);
return Ok("Employee deleted successfully");
}
}


namespace WebApplication5
{



public class Admin
{

public string Username { get; set; }
public string Password { get; set; }

// Constructor to initialize non-nullable properties
//public Admin()
//{
// // Assuming 0 is not a valid ID, you may need to adjust accordingly
// Username = string.Empty;
// Password = string.Empty;
//}
}
}
namespace WebApplication5
{
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Department { get; set; }
public string Email { get; set; }

// Constructor to initialize non-nullable properties
// public Employee()
//{
// Id = 0; // Assuming 0 is not a valid ID, you may need to adjust accordingly
// Name = string.Empty;
// Department = string.Empty;
// Email = string.Empty;
//}
}
}

using System.Collections.Generic;
using System.Configuration.Internal;
using System.Linq;
namespace WebApplication5 {
public class AdminRepository
{
public bool ValidateCredentials(string username, string password)
{
throw new NotImplementedException();
}

public class AdminRepositoryy
{
// Sample data for demonstration purposes (replace with your actual data source)
private readonly List<Admin> _admins = new List<Admin>
{
new Admin { Username = "admin1", Password = "password1" },
new Admin { Username = "admin2", Password = "password2" }
};

public bool ValidateCredentials(string username, string password)
{
// Check if the username and password match any admin in the data source
return _admins.Any(admin => admin.Username == username && admin.Password == password);

}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using WebApplication5;

public class EmployeeRepository
{
private List<Employee> _employees;

public EmployeeRepository()
{
_employees = new List<Employee>();
// Initialize or load employees here
}

public IEnumerable<Employee> GetAllEmployees()
{
return _employees;
}

public Employee GetEmployeeById(int id)
{
return _employees.FirstOrDefault(e => e.Id == id);
}

public void AddEmployee(Employee employee)
{
if (employee != null) // Check for null before adding
{
_employees.Add(employee);
}
else
{
throw new ArgumentNullException(nameof(employee), "Employee cannot be null");
}
}

public void UpdateEmployee(Employee employee)
{
if (employee != null && _employees.Any(e => e.Id == employee.Id)) // Check for null and existence before updating
{
var existingEmployee = _employees.FirstOrDefault(e => e.Id == employee.Id);
if (existingEmployee != null)
{
// Update properties if they are not null
existingEmployee.Name = employee.Name ?? existingEmployee.Name;
existingEmployee.Department = employee.Department ?? existingEmployee.Department;
existingEmployee.Email = employee.Email ?? existingEmployee.Email;
// Update other properties similarly
}
else
{
// Handle not found case
throw new ArgumentException("Employee not found", nameof(employee));
}
}
else
{
// Handle null case or employee not found case
throw new ArgumentNullException(nameof(employee), "Employee cannot be null");
}
}

public void DeleteEmployee(int id)
{
var employeeToRemove = _employees.FirstOrDefault(e => e.Id == id);
if (employeeToRemove != null) // Check for null before removing
{
_employees.Remove(employeeToRemove);
}
else
{
throw new KeyNotFoundException($"Employee with ID {id} not found");
}
}
}

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace WebApplication5
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }


public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<EmployeeRepository>();
services.AddScoped<AdminRepository>();
services.AddControllersWithViews();
}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
     
 
what is notes.io
 

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

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.