public IHttpActionResult CreateSet(string token, JDE_Sets item, int UserId) { if (token != null && token.Length > 0) { var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim()); if (tenants.Any()) { item.TenantId = tenants.FirstOrDefault().TenantId; db.JDE_Sets.Add(item); db.SaveChanges(); JDE_Logs Log = new JDE_Logs { UserId = UserId, Description = "Utworzenie instalacji", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(item) }; db.JDE_Logs.Add(Log); db.SaveChanges(); return(Ok(item)); //return CreatedAtRoute("JDEApi", new {token = token, id = item.SetId }, item); } else { return(NotFound()); } } else { return(NotFound()); } }
public IHttpActionResult EditSet(string token, int id, JDE_Sets item, int UserId) { if (token != null && token.Length > 0) { var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim()); if (tenants.Any()) { var items = db.JDE_Sets.Where(u => u.TenantId == tenants.FirstOrDefault().TenantId&& u.SetId == id); if (items.Any()) { db.Entry(item).State = EntityState.Modified; JDE_Logs Log = new JDE_Logs { UserId = UserId, Description = "Edycja instalacji", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, OldValue = new JavaScriptSerializer().Serialize(items.FirstOrDefault()), NewValue = new JavaScriptSerializer().Serialize(item) }; db.JDE_Logs.Add(Log); try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!JDE_SetsExists(id)) { return(NotFound()); } else { throw; } } } } } return(StatusCode(HttpStatusCode.NoContent)); }
public IHttpActionResult GetPlacesBySetName(string token, string name, int UserId) { if (token != null && token.Length > 0) { var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim()); if (tenants.Any()) { object returned = JDE_SetsExists(name, false); if (Type.GetTypeCode(returned.GetType()) == TypeCode.Int32) { //there's set of given name, return its places var items = (from pl in db.JDE_Places join st in db.JDE_Sets on pl.SetId equals st.SetId join ar in db.JDE_Areas on pl.AreaId equals ar.AreaId join us in db.JDE_Users on pl.CreatedBy equals us.UserId join t in db.JDE_Tenants on pl.TenantId equals t.TenantId where t.TenantId == tenants.FirstOrDefault().TenantId&& st.SetId == (int)returned orderby pl.CreatedOn descending select new { PlaceId = pl.PlaceId, Number1 = pl.Number1, Number2 = pl.Number2, Name = pl.Name, Description = pl.Description, AreaId = ar.AreaId, AreaName = ar.Name, SetId = st.SetId, SetName = st.Name, Priority = pl.Priority, CreatedOn = pl.CreatedOn, CreatedBy = us.UserId, CreatedByName = us.Name + " " + us.Surname, TenantId = t.TenantId, TenantName = t.TenantName, PlaceToken = pl.PlaceToken } ); return(Ok(items)); } else { //there's NO set of given name, create it //But first you have to assign it to "Nieokreślone" Area (create if have to) JDE_Areas nArea; JDE_Sets nSet; JDE_Logs Log; if (!db.JDE_Areas.Where(a => a.Name.Equals("Nieokreślone")).Any()) { //no are like that, let's create it. nArea = new JDE_Areas { Name = "Nieokreślone", Description = "Trafia tu wszystko co zostaje utworzone bez jasno określonego docelowego obszaru", TenantId = tenants.FirstOrDefault().TenantId, CreatedBy = UserId, CreatedOn = DateTime.Now }; db.JDE_Areas.Add(nArea); db.SaveChanges(); Log = new JDE_Logs { UserId = UserId, Description = "Utworzenie obszaru", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(nArea) }; db.JDE_Logs.Add(Log); db.SaveChanges(); } else { //it exists, ret nArea = db.JDE_Areas.Where(a => a.Name.Equals("Nieokreślone")).FirstOrDefault(); } nSet = new JDE_Sets { Name = name.Trim(), Description = null, CreatedBy = UserId, CreatedOn = DateTime.Now, TenantId = tenants.FirstOrDefault().TenantId }; db.JDE_Sets.Add(nSet); db.SaveChanges(); Log = new JDE_Logs { UserId = UserId, Description = "Utworzenie instalacji", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(nSet) }; db.JDE_Logs.Add(Log); db.SaveChanges(); JDE_Places nPlace = new JDE_Places { Name = name.Trim(), Description = null, PlaceToken = Utilities.uniqueToken(), AreaId = nArea.AreaId, SetId = nSet.SetId, CreatedBy = UserId, CreatedOn = DateTime.Now, TenantId = tenants.FirstOrDefault().TenantId }; db.JDE_Places.Add(nPlace); db.SaveChanges(); Log = new JDE_Logs { UserId = UserId, Description = "Utworzenie zasobu", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(nPlace) }; db.JDE_Logs.Add(Log); db.SaveChanges(); var items = (from pl in db.JDE_Places join st in db.JDE_Sets on pl.SetId equals st.SetId join ar in db.JDE_Areas on pl.AreaId equals ar.AreaId join us in db.JDE_Users on pl.CreatedBy equals us.UserId join t in db.JDE_Tenants on pl.TenantId equals t.TenantId where t.TenantId == tenants.FirstOrDefault().TenantId&& st.SetId == nSet.SetId orderby pl.CreatedOn descending select new { PlaceId = pl.PlaceId, Number1 = pl.Number1, Number2 = pl.Number2, Name = pl.Name, Description = pl.Description, AreaId = ar.AreaId, AreaName = ar.Name, SetId = st.SetId, SetName = st.Name, Priority = pl.Priority, CreatedOn = pl.CreatedOn, CreatedBy = us.UserId, CreatedByName = us.Name + " " + us.Surname, TenantId = t.TenantId, TenantName = t.TenantName, PlaceToken = pl.PlaceToken } ); return(Ok(items)); } } else { return(NotFound()); } } else { return(NotFound()); } }