Exemple #1
0
        public virtual IActionResult AddCar([FromBody] Car car)
        {
            (string, UserType)sender;
            try
            {
                sender = Security.SolveGUID(_context, Request.Headers["Guid"]);
            }
            catch (Exception e)
            {
                return(StatusCode(401, e.Message));
            }
            if (!String.IsNullOrEmpty(car.Username) && (!car.Username.Equals(sender.Item1) && sender.Item2 != UserType.WORKSHOP_EMPLOYEE))
            {
                return(StatusCode(403));
            }
            if (String.IsNullOrEmpty(car.Username))
            {
                car.Username = sender.Item1;
            }
            try
            {
                Validator.Validate(_context, car);
            }
            catch (ValidationException e)
            {
                return(StatusCode(400, e.Message));
            }
            car.Owner = _context.Users.FirstOrDefault(x => x.Username.Equals(car.Username));

            DiagnosticProfile diagnosticProfile = new DiagnosticProfile
            {
                LicensePlate = car.LicensePlate
            };

            car.DiagnosticProfile = diagnosticProfile;
            diagnosticProfile.Car = car;
            car.Insurances        = new List <Insurance>();

            _context.Add(car);
            _context.Add(diagnosticProfile);
            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateException e)
            {
                return(StatusCode(400, e.Message + " -> " + e.InnerException.Message));
            }
            return(new ObjectResult(car));
        }
Exemple #2
0
        public virtual IActionResult SetProfile([FromRoute][Required] string licensePlate, [FromBody] DiagnosticProfile body)
        {
            if (string.IsNullOrEmpty(licensePlate))
            {
                return(StatusCode(400));
            }
            (string, UserType)sender;
            try
            {
                sender = Security.SolveGUID(_context, Request.Headers["Guid"]);
            }
            catch (Exception e)
            {
                return(StatusCode(401, e.Message));
            }
            var diagnosticProfile = _context.DiagnosticProfiles.FirstOrDefault(x => x.LicensePlate.Equals(licensePlate));

            if (sender.Item2 != UserType.WORKSHOP_EMPLOYEE)
            {
                return(StatusCode(403));
            }
            if (diagnosticProfile is null)
            {
                return(StatusCode(404));
            }
            if (body.Engine != null)
            {
                diagnosticProfile.Engine = body.Engine;
            }
            if (body.Body != null)
            {
                diagnosticProfile.Body = body.Body;
            }
            if (body.LowVoltage != null)
            {
                diagnosticProfile.LowVoltage = body.LowVoltage;
            }
            if (body.Lighting != null)
            {
                diagnosticProfile.Lighting = body.Lighting;
            }
            if (body.Brakes != null)
            {
                diagnosticProfile.Brakes = body.Brakes;
            }
            if (body.Sensors != null)
            {
                diagnosticProfile.Sensors = body.Sensors;
            }
            if (body.Miscellaneous != null)
            {
                diagnosticProfile.Miscellaneous = body.Miscellaneous;
            }
            if (body.Conditioning != null)
            {
                diagnosticProfile.Conditioning = body.Conditioning;
            }
            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateException)
            {
                return(StatusCode(400));
            }
            return(new ObjectResult(diagnosticProfile));
        }