コード例 #1
0
        public async Task <IHttpActionResult> CreateGeoInfo([FromBody] string ip)
        {
            if (!ModelState.IsValid)
            {
                return(JsonResult.BadRequest("IP address has to be a string"));
            }
            if (!ValidateIPv4(ip))
            {
                return(JsonResult.BadRequest("Provided IP address is in a wrong format!"));
            }
            try
            {
                var exists = await GeoInfoExists(ip);

                if (exists)
                {
                    return(JsonResult.BadRequest("IP address already exists in the DB!"));
                }

                GeoInfo geoInfo = new GeoInfo();
                geoInfo = await FillWithDetails(ip);

                db.GeoInfos.Add(geoInfo);
                await db.SaveChangesAsync();

                return(JsonResult.Success(Mapper.Map <GeoInfo, GeoInfoDto>(geoInfo)));
            }
            catch (Exception e)
            {
                return(JsonResult.Error(string.Format("Could not create GeoInfo with ip:{0} due to error: {1}", ip, e.Message)));
            }
        }