public Subvention Put(int id, [FromBody] Subvention recSubvention) { Subvention returnSubvention = null; try { returnSubvention = subventionController.PutSubvention(id, recSubvention); Response.StatusCode = 200; } catch (Exception ex) { Response.StatusCode = 500; Response.WriteAsync(ex.Message); } return(returnSubvention); }
/// <summary> /// updates a subvention in DB /// </summary> /// <param name="id"></param> /// <param name="recSub"></param> public Subvention PutSubvention(int id, Subvention recSub) { var putSubvention = entities.Subventions.Where(x => x.Id == id).FirstOrDefault(); if (putSubvention != null) { putSubvention.Name = recSub.Name; putSubvention.Percentage = recSub.Percentage; putSubvention.Amount = recSub.Amount; entities.SaveChanges(); return(entities.Subventions.Where(x => x.Id == id).FirstOrDefault()); } else { throw new EntryCouldNotBeFoundException("Could not find subvention in database"); } }
/// <summary> /// Reads alle data from the Datasorce .xls FIle and creates new Subvention Objects with the data from the file. /// Adds all the Subventions Objects into the sublist. /// </summary> /// <returns>List of Subventios from the .xls file</returns> public List <Subvention> ReadSubventionSource() { List <Subvention> subList = new List <Subvention>(); int id = 1; ISheet sheet; Stream templateStream = new MemoryStream(); using (var file = new FileStream(@"DataSources/zuschuesse-religions-und-weltanschauungsunterricht.xls", FileMode.Open, FileAccess.Read)) { HSSFWorkbook hssfwb = new HSSFWorkbook(file); sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook IRow headerRow = sheet.GetRow(0); //Get Header Row int cellCount = headerRow.LastCellNum; for (int i = (2); i <= sheet.LastRowNum; i++) //Read Excel File { IRow row = sheet.GetRow(i); if (row == null) { continue; } if (row.Cells.All(d => d.CellType == CellType.Blank)) { continue; } for (int l = 2; l < row.Cells.Count; l++) { int year = 0; switch (l) { case 2: year = 2011; break; case 3: year = 2012; break; case 4: year = 2013; break; case 5: year = 2014; break; case 6: year = 2015; break; case 7: year = 2016; break; default: break; } Subvention s = new Subvention(id, Convert.ToDouble(row.Cells[l].ToString()), year, row.Cells[1].ToString().Trim(), Convert.ToInt32(row.Cells[0].ToString())); id++; subList.Add(s); } } return(subList); } }
/// <summary> /// inserts a subvention in DB and returns created subvention (including auto-generated id) /// </summary> /// <param name="recSub"></param> /// <returns></returns> public Subvention PostSubvention(Subvention recSub) { entities.Subventions.Add(new Subvention(recSub.Name, recSub.Percentage, recSub.Amount)); entities.SaveChanges(); return(entities.Subventions.OrderByDescending(x => x.Id).FirstOrDefault()); }