public static async Task <ErrorModel> AddVintage(long wineId, string year) { var isNumber = int.TryParse(year, out int yearInt); if (!isNumber) { return new ErrorModel { ErrorCode = false, Message = "Du får skriva bara siffror", Object = null } } ; var url = Links.baseLink + Links.vintages; var payload = new AddVintageModel { WineId = wineId, Year = yearInt }; var token = await GetToken(); var responseBody = await RestVerbs.Post(url, payload, token); if (string.IsNullOrEmpty(responseBody)) { return new ErrorModel { ErrorCode = false, Message = "Årgangen har inte lagts till", Object = new VintageResponse { VintageId = 0, WineId = 0, Year = "0" } } } ; try { var responseBodyJson = JsonConvert.DeserializeObject <VintageResponse>(responseBody); if (responseBodyJson.WineId == 0 || responseBodyJson.VintageId == 0) { return new ErrorModel { ErrorCode = false, Message = "Årgangen har inte lagts till", Object = new VintageResponse { VintageId = 0, WineId = 0, Year = "0" } } } ; return(new ErrorModel { ErrorCode = true, Message = null, Object = responseBodyJson }); } catch (Exception error) { return(new ErrorModel { ErrorCode = false, Message = error.Message, Object = null }); } }
public static async Task <ErrorModel> AddInventory(long vintageId, long shelfId, int amount) { var url = Links.baseLink + Links.inventories; var payload = new AddInventoryModel { VintageId = vintageId, ShelfId = shelfId, Amount = amount }; var token = await GetToken(); var responseBody = await RestVerbs.Post(url, payload, token); if (string.IsNullOrEmpty(responseBody)) { return new ErrorModel { ErrorCode = false, Message = "Inventeringen har inte lagts till", Object = new InventoryResponse { InventoryId = 0, ShelfId = 0, Amount = 0 } } } ; try { var responseBodyJson = JsonConvert.DeserializeObject <InventoryResponse>(responseBody); return(new ErrorModel { ErrorCode = true, Message = null, Object = responseBodyJson }); } catch (Exception error) { return(new ErrorModel { ErrorCode = false, Message = error.Message, Object = null }); } }
public static async Task <ErrorModel> AddDistrict(string districtName, long regionId) { var url = Links.baseLink + Links.metadata + '/' + Links.districts; var payload = new AddDistrictModel { DistrictName = districtName, RegionId = regionId }; var token = await GetToken(); var responseBody = await RestVerbs.Post(url, payload, token); if (string.IsNullOrEmpty(responseBody)) { return new ErrorModel { ErrorCode = false, Message = "Distrikten har inte lagts till", Object = new DistrictResponse { DistrictId = 0, DistrictName = "", RegionId = 0 } } } ; try { var responseBodyJson = JsonConvert.DeserializeObject <DistrictResponse>(responseBody); return(new ErrorModel { ErrorCode = true, Message = null, Object = responseBodyJson }); } catch (Exception error) { return(new ErrorModel { ErrorCode = false, Message = error.Message, Object = null }); } }
public static async Task <ErrorModel> AddCountry(string countryName) { var url = Links.baseLink + Links.metadata + '/' + Links.countries; var payload = new AddCountryModel { CountryName = countryName }; var token = await GetToken(); var responseBody = await RestVerbs.Post(url, payload, token); if (string.IsNullOrEmpty(responseBody)) { return new ErrorModel { ErrorCode = false, Message = "Landet har inte lagts till", Object = new CountryResponse { CountryId = 0, CountryName = "" } } } ; try { var responseBodyJson = JsonConvert.DeserializeObject <CountryResponse>(responseBody); return(new ErrorModel { ErrorCode = true, Message = null, Object = responseBodyJson }); } catch (Exception error) { return(new ErrorModel { ErrorCode = false, Message = error.Message, Object = null }); } }
public static async Task <ErrorModel> AddShelf(string shelfName) { var url = Links.baseLink + Links.shelves; var payload = new AddShelfModel { ShelfName = shelfName }; var token = await GetToken(); var responseBody = await RestVerbs.Post(url, payload, token); if (string.IsNullOrEmpty(responseBody)) { return new ErrorModel { ErrorCode = false, Message = "Hyllan har inte lagts till", Object = new ShelfResponse { ShelfId = 0, Name = null } } } ; try { var responseBodyJson = JsonConvert.DeserializeObject <ShelfResponse>(responseBody); return(new ErrorModel { ErrorCode = true, Message = null, Object = responseBodyJson }); } catch (Exception error) { return(new ErrorModel { ErrorCode = false, Message = error.Message, Object = null }); } }
public static async Task <ErrorModel> AddWine(string winName, string producer, long districtId, string alcohol, List <WineGrape> AddedGrapes, Image image) { double alcoholDouble = 0; if (!string.IsNullOrEmpty(alcohol)) { var isNumber = double.TryParse(alcohol, out alcoholDouble); if (!isNumber) { return new ErrorModel { ErrorCode = false, Message = "alkoholhalt måste vara en siffra!", Object = null } } ; if (alcoholDouble < 0 || alcoholDouble > 50) { return new ErrorModel { ErrorCode = false, Message = "alkoholhalt värde är inte rimligt!", Object = null } } ; } var url = Links.baseLink + Links.wines; for (int i = 0; i < AddedGrapes.Count(); i++) { AddedGrapes[i].Grape = null; } //var imageToByteArray(image) var payload = new AddWine { Name = winName, DistrictId = districtId, Producer = producer, Alcohol = alcoholDouble, WineGrapes = AddedGrapes, Image = imageToByteArray(image) }; var token = await GetToken(); var responseBody = await RestVerbs.Post(url, payload, token); if (string.IsNullOrEmpty(responseBody)) { return new ErrorModel { ErrorCode = false, Message = "Vinet har inte lagts till", Object = new VintageResponse { VintageId = 0, WineId = 0, Year = "0" } } } ; return(new ErrorModel { ErrorCode = true, Message = null, Object = null }); //try //{ // var responseBodyJson = JsonConvert.DeserializeObject<VintageResponse>(responseBody); // if (responseBodyJson.WineId == 0 || responseBodyJson.VintageId == 0) // return new ErrorModel { ErrorCode = false, Message = "Årgangen har inte lagts till", Object = new VintageResponse { VintageId = 0, WineId = 0, Year = "0" } }; // return new ErrorModel { ErrorCode = true, Message = null, Object = responseBodyJson }; //} //catch (Exception error) //{ // return new ErrorModel { ErrorCode = false, Message = error.Message, Object = null }; //} }