
public class SpencesController : ApiController
{
private SpencesEntities db = new SpencesEntities();
public IQueryable<Spences> Get()
{
return db.Spences.Where(x => x.CreatedInApp == true & x.SpenceId > 1);
}
}
public IQueryable<Spences> Get(int year)
{
return db.Spences.Where(x => x.Spence > 0 & x.SpenceId > 1 & x.YSpences == year);
}
---------------------------------------------------------------------
public HttpResponseMessage GetSpenceSum(int yearT)
{
try
{
using (Models.SpencesEntities db = new Models.SpencesEntities())
{
var spenceSum = db.Spences
.Where(x => x.YSpences == yearT & x.SpenceId > 1)
.Sum(x => x.Spence);
// Create an anonymous object with the sum value
var responseObject = new { Spence = spenceSum };
// Serialize the object to JSON format
var json = JsonConvert.SerializeObject(responseObject);
// Create a response message with the JSON content
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StringContent(json, Encoding.UTF8, "application/json");
return response;
}
}
catch (Exception ex)
{
// Handle exceptions and return an error response
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
-------------------------------------------------------------------------
public HttpResponseMessage Get(Byte spenceId, Int16 year, Byte monthNumber)
{
using (Models.SpencesEntities db = new Models.SpencesEntities())
{
{
//return Request.CreateResponse(HttpStatusCode.OK, db.BuySell.Where(x => x.Nivel == level).Select(y => new BuySell.Models.BuySell
return Request.CreateResponse(HttpStatusCode.OK, db.Spences.Where(x => x.SpenceId == spenceId & x.YSpences == year & x.MonthNumber == monthNumber).GroupBy(x => x.MonthNumber & x.YSpences).Select(y => new SpencesViewModel
{
id = db.Spences.Where(x => x.SpenceId == spenceId & x.YSpences == year & x.MonthNumber == monthNumber).Count(),
SpenceId = spenceId,
DSpences = db.Spences.Where(x => x.SpenceId == spenceId & x.YSpences >= year & x.MonthNumber == monthNumber).Min(x => x.DSpences),
dia = db.Spences.Where(x => x.SpenceId == spenceId & x.YSpences >= year & x.MonthNumber == monthNumber).Min(x => x.dia),
DiaNumber = db.Spences.Where(x => x.SpenceId == spenceId & x.YSpences >= year & x.MonthNumber == monthNumber).Min(x => x.DiaNumber),
DiaSpences = db.Spences.Where(x => x.SpenceId == spenceId & x.YSpences >= year & x.MonthNumber == monthNumber).Min(x => x.DiaSpences),
MSpences = db.Spences.Where(x => x.SpenceId == spenceId & x.YSpences >= year & x.MonthNumber == monthNumber).Max(x => x.MSpences),
MonthNumber = monthNumber,
YSpences = year,
Spence = db.Spences.Where(x => x.SpenceId == spenceId & x.YSpences == year & x.MonthNumber == monthNumber).Sum(x => x.Spence),
CreatedInApp = false
}).ToList());
}
}
}
[System.Web.Http.HttpPost]
public IHttpActionResult Add(Models.ViewModels.SpencesViewModel modelSpences)
{
using (SpencesEntities db = new SpencesEntities())
{
var oSpences = new Models.Spences();
//oSpences.id = modelSpences.id;
oSpences.SpenceId = modelSpences.SpenceId;
oSpences.Spence = modelSpences.Spence;
oSpences.DSpences = modelSpences.DSpences;
oSpences.dia = modelSpences.dia;
oSpences.DiaNumber = modelSpences.DiaNumber;
oSpences.DiaSpences = modelSpences.DiaSpences;
oSpences.MSpences = modelSpences.MSpences;
oSpences.MonthNumber = modelSpences.MonthNumber;
oSpences.YSpences = modelSpences.YSpences;
oSpences.CreatedInApp = modelSpences.CreatedInApp;
oSpences.SelfLoans_AfterSync = modelSpences.SelfLoans_AfterSync;
oSpences.FechaSync = modelSpences.FechaSync;
oSpences.DebitUniqueIdentifier = modelSpences.DebitUniqueIdentifier;
oSpences.Comment = modelSpences.Comment;
db.Spences.Add(oSpences);
db.SaveChanges();
}
return Ok("Exito");
}
---------------------------------------------------------------------------------------------------------
{
"id": 1,
"SpenceId": 64,
"Spence": 1.0,
"DSpences": "2024-11-18T09:29:43.0913073-05:00",
"dia": "sample string 2",
"DiaNumber": 64,
"DiaSpences": 64,
"MSpences": "sample string 3",
"MonthNumber": 64,
"YSpences": 1,
"CreatedInApp": true,
"BankDebit": true,
"SelfLoans_AfterSync": true,
"FechaSync": "2024-11-18T09:29:43.0922999-05:00",
"DebitUniqueIdentifier": "sample string 4",
"Comment": "test 999 UDEMY"
}
// PUT api/Spences/5
[ResponseType(typeof(void))]
public IHttpActionResult Put(int id, Spences spences)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != spences.id)
{
return BadRequest();
}
db.Entry(spences).State = System.Data.Entity.EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!itemExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
private bool itemExists(int id)
{
return db.Spences.Count(e => e.id == id) > 0;
}
------------------------------------------------------------------------------------------------------
{
"id": 284,
"SpenceId": 64,
"Spence": 1.0,
"DSpences": "2024-11-18T09:29:43.0913073-05:00",
"dia": "sample string 2",
"DiaNumber": 64,
"DiaSpences": 64,
"MSpences": "sample string 3",
"MonthNumber": 64,
"YSpences": 1,
"CreatedInApp": true,
"BankDebit": true,
"SelfLoans_AfterSync": true,
"FechaSync": "2024-11-18T09:29:43.0922999-05:00",
"DebitUniqueIdentifier": "sample string 4",
"Comment": "test 888 UDEMY"
}
public HttpResponseMessage DeleteSpences(int spenceId, DateTime day)
{
try
{
using (SpencesEntities spenceEntities = new SpencesEntities())
{
var entitySpences = spenceEntities.Spences.FirstOrDefault(e => e.SpenceId == spenceId & e.DSpences == day);
if (entitySpences == null)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Expense does not exist!");
}
else
{
spenceEntities.Spences.Remove(entitySpences);
spenceEntities.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK);
}
}
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
Password to the Software after restoring database = ciro
In this comprehensive tutorial course, you will learn how to build a fully functional REST API from scratch using Visual Studio, ASP.NET, and C#. Whether you're a beginner or already have some experience in software development, this course will guide you step by step through the entire process, from setting up your development environment to understanding how a complete API works in a real-world scenario.
We begin by introducing the fundamentals of RESTful architecture and API design principles. Then, you will move into the practical implementation phase, where you’ll learn how to create a REST API using ASP.NET and C#. Key concepts such as routing, controllers, and HTTP methods (GET, POST, PUT, DELETE) are explained clearly and implemented step by step. We also cover data handling using Entity Framework to ensure efficient communication between the API and SQL Server databases.
A key highlight of this course is that you will receive access to a fully functional REST API available for download that includes more than 100 methods already implemented. Once configured, students can perform all the testing they wish, including both reading and writing operations using methods such as PUT and POST. This allows you to experiment freely, understand how each endpoint behaves, and analyze how the different layers of the system interact with each other.
Additionally, you will have access to a pre-built SQL Server database and a fully functional desktop application connected to the API. This gives you a complete ecosystem to study, modify, and integrate into your own projects, helping you gain practical, real-world experience.
The course also includes a dedicated section on testing and troubleshooting. You will learn how to use tools like Postman to perform API testing, validate responses, and debug common issues effectively.
It is important to note that this course is structured as a tutorial-based learning experience. It does not include exercises or assignments; instead, it focuses entirely on step-by-step video tutorials where you can follow along and replicate the implementation at your own pace.
By the end of the course, you will have the confidence and practical knowledge needed to build, test, and analyze REST APIs using ASP.NET and C#, while working with a fully implemented, downloadable API that you can continue exploring and expanding in your own projects.