Ejemplo n.º 1
0
        public async Task <IActionResult> UpdateCar(int id, [FromBody] Car body)
        {
            Console.WriteLine("Controller: Update car with id:" + id);
            await Db.Connection.OpenAsync();

            var query  = new DatabaseQuery(Db);
            var result = await query.FindOneCarAsync(id);

            if (result is null)
            {
                return(new NotFoundResult());
            }
            //check if new licenace is in database - if so, return.
            var result2 = await query.FindOneCarAsync(body.LicensePlate);

            if ((result != null) && result2.Id != result.Id)
            {
                return(Content("{\"status\":\"Error License plate in DB!\"}"));
            }

            //insert type id to list if needed and return the id of updated type
            int type_id = await query.GetTypeID(body.CarType);

            body.carTypeId = type_id;
            body.Db        = Db;
            //copy and update.
            result.Copy(body);
            await result.UpdateAsync();

            return(new OkObjectResult("{\"status\":\"OK\"}"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> GetOneCar(string license_plate)
        {
            Console.WriteLine("Controller: Get one car:" + license_plate);
            await Db.Connection.OpenAsync();

            var query  = new DatabaseQuery(Db);
            var result = await query.FindOneCarAsync(license_plate);

            if (result is null)
            {
                return(new NotFoundResult());
            }
            return(new OkObjectResult(result));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> DeleteOneCar(int id)
        {
            Console.WriteLine("Controller: Delete car with id:" + id);
            await Db.Connection.OpenAsync();

            var query  = new DatabaseQuery(Db);
            var result = await query.FindOneCarAsync(id);

            if (result is null)
            {
                return(new NotFoundResult());
            }
            result.Db = Db;
            await result.DeleteAsync();

            return(new OkResult());
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> AddCar([FromBody] Car body)
        {
            Console.WriteLine("Controller: Add car with license plate:" + body.LicensePlate);
            await Db.Connection.OpenAsync();

            //check if already in database - if so, return.
            var query  = new DatabaseQuery(Db);
            var result = await query.FindOneCarAsync(body.LicensePlate);

            if (result != null)
            {
                return(Content("{\"status\":\"Error License plate in DB!\"}"));
            }
            //insert type id to list if needed and return its id
            int type_id = await query.GetTypeID(body.CarType);

            body.carTypeId = type_id;
            //add to db
            body.Db = Db;
            await body.InsertAsync();

            return(Content("{\"status\":\"OK\"}"));
        }