// POST: api/Absenta public HttpResponseMessage Post(HttpRequestMessage request) { var msg = new HttpResponseMessage(); try { var value = request.Content.ReadAsStringAsync().Result; AbsentaDTO absenta = JsonConvert.DeserializeObject <AbsentaDTO>(value); t_absenta absentanoua = Mapper.Map <AbsentaDTO, t_absenta>(absenta); catalog.Absente.Add(absentanoua); catalog.SaveChanges(); msg.StatusCode = System.Net.HttpStatusCode.OK; msg.Content = new StringContent("O absenta noua a fost adaugata!"); } catch (Exception ex) { msg.StatusCode = System.Net.HttpStatusCode.BadRequest; msg.Content = new StringContent("Nu s-a putut adauga o absenta noua!"); } return(msg); }
public Task <AbsentaDTO> GetAbsentaAsync(int _absentaID) { return(Task.Factory.StartNew(() => { var requestLink = "/Absenta/"; var uri = new Uri(WebSiteAPI + requestLink + _absentaID.ToString()); var response = _client.GetAsync(uri).Result; if (response.IsSuccessStatusCode) { var content = response.Content.ReadAsStringAsync().Result; AbsentaDTO absentaCautata = JsonConvert.DeserializeObject <AbsentaDTO>(content); return absentaCautata; } return null; })); }
// PUT: api/Absenta/5 public HttpResponseMessage Put(int id, HttpRequestMessage request) { var msg = new HttpResponseMessage(); try { var value = request.Content.ReadAsStringAsync().Result; t_absenta absenta = catalog.Absente.Where(absentacautata => absentacautata.Id == id).FirstOrDefault(); AbsentaDTO absentanoua = JsonConvert.DeserializeObject <AbsentaDTO>(value); absenta.Id = absentanoua.Id; absenta.Data = absentanoua.Data; //absenta.ElevID = absentanoua.ElevID; //absenta.Elev = absentanoua.Elev; //absenta.MaterieID = absentanoua.MaterieID; //absenta.Materie = absentanoua.Materie; t_elev elev = catalog.Elevi.Where(elevcautat => elevcautat.Id == absentanoua.ElevID).FirstOrDefault(); absenta.Elev = elev; t_materie materie = catalog.Materii.Where(materiecautata => materiecautata.Id == absentanoua.MaterieID).FirstOrDefault(); absenta.Materie = materie; //Lista de elevi catalog.SaveChanges(); msg.StatusCode = System.Net.HttpStatusCode.OK; msg.Content = new StringContent("Modificarile au fost procesate cu succes!"); } catch (Exception ex) { msg.StatusCode = System.Net.HttpStatusCode.BadRequest; msg.Content = new StringContent("Nu s-au putut executa modificarile dorite!"); } return(msg); }
public Task <string> PostAbsentaAsync(AbsentaDTO absentaNoua) { return(Task.Factory.StartNew(() => { var requestLink = "/Absenta"; var uri = new Uri(WebSiteAPI + requestLink); var myContent = JsonConvert.SerializeObject(absentaNoua); var buffer = Encoding.UTF8.GetBytes(myContent); var byteContent = new ByteArrayContent(buffer); var response = _client.PostAsync(uri, byteContent).Result; if (response.IsSuccessStatusCode) { var content = response.Content.ReadAsStringAsync().Result; return content; } return null; })); }