Exemplo n.º 1
0
        public HttpResponseMessage Get(string type, decimal price, int?modelYear = null, float?squareMeters = null)
        {
            Decimal insuranceValue = 0;
            //Validate token
            var     re      = Request;
            var     headers = re.Headers;
            Company company;

            if (headers.Contains("ServiceToken"))
            {
                string token = headers.GetValues("ServiceToken").First();

                company = companyService.GetCompanyByToken(token);

                if (company == null)
                {
                    return(this.Request.CreateResponse(
                               HttpStatusCode.BadRequest,
                               new { Message = "Invalid ServiceToken." }));
                }
            }
            else
            {
                return(this.Request.CreateResponse(
                           HttpStatusCode.BadRequest,
                           new { Message = "Missing ServiceToken. Please register your company to use our web services." }));
            }

            //Validate insurance type
            var insuranceType = insuranceTypeService.GetInsuranceTypes(type).FirstOrDefault();

            if (insuranceType == null)
            {
                return(this.Request.CreateResponse(
                           HttpStatusCode.BadRequest,
                           new { Message = "Invalid insurance type." }));
            }


            switch (insuranceType.Name)
            {
            case "Car":
                if (!modelYear.HasValue)
                {
                    return(this.Request.CreateResponse(
                               HttpStatusCode.BadRequest,
                               new { Message = "Missing model year." }));
                }

                insuranceValue = insuranceService.CalcInsuranceCar(price, modelYear.Value);

                break;

            case "Motorcycle":
                if (!modelYear.HasValue)
                {
                    return(this.Request.CreateResponse(
                               HttpStatusCode.BadRequest,
                               new { Message = "Missing model year." }));
                }

                insuranceValue = insuranceService.CalcInsuranceMotorcycle(price, modelYear.Value);
                break;

            case "House":

                insuranceValue = insuranceService.CalcInsuranceHouse(price);
                break;

            case "Farm":
                if (!squareMeters.HasValue)
                {
                    return(this.Request.CreateResponse(
                               HttpStatusCode.BadRequest,
                               new { Message = "Missing square meters." }));
                }

                insuranceValue = insuranceService.CalcInsuranceFarm(price, squareMeters.Value);
                break;

            default:

                break;
            }

            companyLogService.CreateCompanyLog(new CompanyLog()
            {
                Company    = company.Name,
                InsureType = insuranceType.Name
            });

            return(this.Request.CreateResponse(
                       HttpStatusCode.OK,
                       new
            {
                Company = company.Name,
                Type = insuranceType.Name,
                Value = insuranceValue
            }));
        }