public HttpResponseMessage Post(GroupManagmentViewModel obj) { try { obj.ErrorMessage = String.Empty; obj.LoggedUser = Person.GetLoggedPerson(User); if (obj.LoggedUser.Status == StatusEnum.Deleted) { obj.ErrorMessage = "Uprawnienia uzytkownika wygasly!"; return(Request.CreateResponse(HttpStatusCode.Created, obj)); } if (obj.CurrentOrganization == null) { obj.CurrentOrganization = db.Organizations.FirstOrDefault(x => x.OrganizationID == obj.LoggedUser.OrganizationID); } if (obj.CurrentOrganization == null) { obj.ErrorMessage = "Brak organizacji do ktorej mozna przypisac grupe!"; return(Request.CreateResponse(HttpStatusCode.Created, obj)); } switch (obj.ActionType) { case BaseActionType.Get: //get groups assigned to organizaction obj.Current = new ProfileGroup(); var groups = (from grp in db.GroupsInOrganizations join g in db.Groups on grp.ProfileGroupID equals g.ProfileGroupID where grp.OrganizationID == obj.CurrentOrganization.OrganizationID && g.Name != "Wszyscy" && !g.IsDeleted orderby g.CreateDate descending select new { ProfileGroupID = g.ProfileGroupID, Name = g.Name } ).ToList(); obj.Groups = new List <ProfileGroup>(); if (groups.Any()) { obj.Groups = (from grp in groups group grp by grp.ProfileGroupID into gp select new ProfileGroup { ProfileGroupID = gp.Key, Name = groups.FirstOrDefault(x => x.ProfileGroupID == gp.Key).Name }).ToList(); } foreach (var g in obj.Groups) { g.AssignedPeople = (from pig in db.PeopleInGroups join p in db.Users on pig.PersonID equals p.Id where pig.ProfileGroupID == g.ProfileGroupID select p).ToList(); } obj.Success = String.Empty; break; case BaseActionType.Delete: obj.Current.IsDeleted = true; obj.Current.DeletedUserID = obj.LoggedUser.Id; obj.Current.DeletedDate = DateTime.Now; var personsInGroups = (from t in db.PeopleInGroups where t.ProfileGroupID == obj.Current.ProfileGroupID select t).ToList(); if (personsInGroups != null && personsInGroups.Any()) { db.PeopleInGroups.RemoveRange(personsInGroups); } var current = obj.Groups.FirstOrDefault(x => x.ProfileGroupID == obj.Current.ProfileGroupID); if (current != null) { obj.Groups.Remove(current); } db.SaveChanges(); obj.Success = "Dane usuniete!"; break; case BaseActionType.Edit: var group = db.Groups.FirstOrDefault(x => x.ProfileGroupID == obj.Current.ProfileGroupID); group.Name = obj.Current.Name; var toRemove = (from t in db.PeopleInGroups where t.ProfileGroupID == obj.Current.ProfileGroupID select t).ToList(); if (toRemove != null && toRemove.Any()) { db.PeopleInGroups.RemoveRange(toRemove); db.SaveChanges(); } if (obj.Current.AssignedPeople != null && obj.Current.AssignedPeople.Any()) { foreach (var item in obj.Current.AssignedPeople) { var pg = new ProfileGroup2Person(); pg.IsDeleted = false; pg.PersonID = item.Id; pg.ProfileGroupID = obj.Current.ProfileGroupID; db.PeopleInGroups.Add(pg); } db.SaveChanges(); } db.Entry(group).State = EntityState.Modified; db.SaveChanges(); obj.Success = "Dane zapisane!"; break; case BaseActionType.Add: obj.Current.CreateDate = DateTime.Now; obj.Current.CreateUserID = obj.LoggedUser.Id; obj.Current.IsDeleted = false; db.Groups.Add(obj.Current); var gip = new ProfileGroup2Organization(); gip.OrganizationID = obj.CurrentOrganization.OrganizationID; gip.ProfileGroupID = obj.Current.ProfileGroupID; db.GroupsInOrganizations.Add(gip); db.SaveChanges(); //assigned people to group if (obj.Current.AssignedPeople != null) { foreach (var item in obj.Current.AssignedPeople) { var pg = new ProfileGroup2Person(); pg.IsDeleted = false; pg.PersonID = item.Id; pg.ProfileGroupID = obj.Current.ProfileGroupID; db.PeopleInGroups.Add(pg); } db.SaveChanges(); } obj.Current = new ProfileGroup(); obj.ActionType = BaseActionType.Get; Post(obj); obj.Success = "Dane zapisane!"; break; default: break; } return(Request.CreateResponse(HttpStatusCode.Created, obj));; } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }
public HttpResponseMessage Post(OrganizationViewModel obj) { try { obj.ErrorMessage = String.Empty; if (obj.LoggedUser == null) { obj.LoggedUser = Person.GetLoggedPerson(User); } switch (obj.ActionType) { case BaseActionType.Get: obj.Organizations = db.Organizations.OrderBy(x => x.CreateDate).ToList(); if (obj.Organizations != null) { var currentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 0, 0); currentDate = currentDate.AddHours(-72); foreach (var org in obj.Organizations) { if (String.IsNullOrEmpty(org.NewName)) { org.NewName = org.Name; } if (!org.ChangeNameDate.HasValue || org.ChangeNameDate.Value <= currentDate) { org.NewName = org.Name; } } } obj.Success = String.Empty; break; case BaseActionType.Delete: obj.Current.IsDeleted = false; obj.Current.DeletedUserID = obj.LoggedUser.Id; obj.Current.Status = OrganizationEnum.Hidden; obj.Current.DeletedDate = DateTime.Now; db.Entry(obj.Current).State = EntityState.Modified; db.SaveChanges(); var deleted = obj.Organizations.FirstOrDefault(x => x.OrganizationID == obj.Current.OrganizationID); deleted.Status = OrganizationEnum.Hidden; obj.Success = "Organizacja została ukryta!"; break; case BaseActionType.Edit: db.Entry(obj.Current).State = EntityState.Modified; db.SaveChanges(); obj.Success = "Dane organizacji zostaly zmienione!"; break; case BaseActionType.Add: obj.Current.CreateUserID = obj.LoggedUser.Id; obj.Current.CreateDate = DateTime.Now; obj.Current.IsDeleted = false; obj.Current.Status = OrganizationEnum.Active; obj.Current.UpdateSecurityStamp(); if (ModelState.IsValid) { db.Organizations.Add(obj.Current); db.SaveChanges(); if (obj.Current.IsTrainingAvailableForAll) { var group = db.Groups.FirstOrDefault(x => x.Name == "Wszyscy"); if (group == null) { obj.ErrorMessage = "Brak grupy Wszyscy. Prosze o kontakt z administratorem!"; return(Request.CreateResponse(HttpStatusCode.Created, obj));; } var pgo = new ProfileGroup2Organization(); pgo.ProfileGroupID = group.ProfileGroupID; pgo.OrganizationID = obj.Current.OrganizationID; db.GroupsInOrganizations.Add(pgo); db.SaveChanges(); } } MailMessage mail = new MailMessage(new MailAddress(Helpers.GetMailFrom(MailAccount.EVENT), "Kenpro"), new MailAddress("*****@*****.**")) { Subject = "Utworzenie organizacji", Body = string.Format("W dniu {0} została utworzona nowa organizacja {1} przez {2}", DateTime.Now.ToString("dd/MM/yyyy"), obj.Current.Name, obj.LoggedUser.DisplayName), IsBodyHtml = true }; mail.BodyEncoding = UTF8Encoding.UTF8; mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; Mail.Send(mail, MailAccount.EVENT); LogService.OrganizationLogs(SystemLog.OrganizationCreate, db, obj.Current.Name, obj.LoggedUser.Id); obj.ActionType = BaseActionType.GetExtData; Post(obj); obj.Success = "Organizacja zostala zapisana!"; break; case BaseActionType.GetSimple: obj.Organizations = (from t in db.Organizations where t.Status != OrganizationEnum.Deleted orderby t.CreateDate descending select t).ToList(); if (obj.Organizations != null) { var currentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 0, 0); currentDate = currentDate.AddHours(-72); foreach (var org in obj.Organizations) { if (String.IsNullOrEmpty(org.NewName)) { org.NewName = org.Name; } if (!org.ChangeNameDate.HasValue || org.ChangeNameDate.Value <= currentDate) { org.NewName = org.Name; } } } obj.Success = String.Empty; break; case BaseActionType.GetExtData: //get settingds obj.Setting = db.AppSettings.FirstOrDefault(x => x.IsDefault); if (obj.Setting == null) { obj.Setting = new AppSetting(); obj.Setting.AllowUserToChangeName = true; obj.Setting.AllowUserToChangeMail = true; obj.Setting.SpaceDisk = 50; obj.Setting.MaxAssignedUser = 10; obj.Setting.IsGlobalAvailable = true; obj.Setting.IsTrainingAvailableForAll = true; obj.Setting.MaxActiveTrainings = 5; obj.Setting.DefaultEmail = "*****@*****.**"; obj.Setting.DefaultName = "Kenpro"; obj.Setting.IsDefault = true; db.AppSettings.Add(obj.Setting); db.SaveChanges(); } obj.Current = new Organization(); obj.Current.SpaceDisk = obj.Setting.SpaceDisk; obj.Current.MaxAssignedUser = obj.Setting.MaxAssignedUser; obj.Current.IsGlobalAvailable = obj.Setting.IsGlobalAvailable; obj.Current.IsTrainingAvailableForAll = obj.Setting.IsTrainingAvailableForAll; obj.Current.CanUserChangeMail = obj.Setting.AllowUserToChangeMail; obj.Current.CanUserChangeName = obj.Setting.AllowUserToChangeName; break; case BaseActionType.ById: obj.Detail = new OrganizationDto(); if (obj.OrganizationID == 0) { obj.ErrorMessage = "Problem z pobraniem szczegowych danych organizacji"; return(Request.CreateResponse(HttpStatusCode.Created, obj)); } var current = obj.Organizations.FirstOrDefault(x => x.OrganizationID == obj.OrganizationID); if (String.IsNullOrEmpty(current.NewName)) { current.NewName = current.Name; } var protector = db.Users.FirstOrDefault(x => x.Id == current.ProtectorID); var assignedUsers = (from u in db.Users where u.OrganizationID.HasValue && u.OrganizationID.Value == obj.OrganizationID && u.Profile != ProfileEnum.Protector select u).ToList(); obj.Detail.AssignedUser = assignedUsers.Count(x => x.Status == StatusEnum.Active); obj.Detail.BlockedUser = assignedUsers.Count(x => x.Status == StatusEnum.Blocked); obj.Detail.DeleteUser = assignedUsers.Count(x => x.Status == StatusEnum.Deleted); obj.Detail.InvationUser = assignedUsers.Count(x => x.Status == StatusEnum.Invited); obj.Detail.Email = protector != null ? protector.Email : String.Empty; obj.Detail.Login = protector != null ? protector.DisplayName : String.Empty; var trainings = (from tio in db.TrainingsInOrganizations join t in db.Trainings on tio.TrainingID equals t.TrainingID join td in db.TrainingDetails on t.TrainingID equals td.TrainingID where tio.OrganizationID == obj.OrganizationID select td).ToList(); obj.Detail.UsedSpaceDisk = trainings.Sum(x => x.FileSize); //todo details obj.Current = current; obj.Success = String.Empty; break; case BaseActionType.GetSpecial: obj.Protector = new Person(); obj.Protector.Profile = ProfileEnum.Protector; var setting = db.AppSettings.FirstOrDefault(x => x.IsDefault); if (setting != null) { obj.Protector.UserName = setting.DefaultName; obj.Protector.Email = setting.DefaultEmail; } obj.NotAssigned = (from t in db.Organizations where t.ProtectorID == null && t.Status == OrganizationEnum.Active select new OrganizationDto { Id = t.OrganizationID, Name = t.Name }).ToList(); if (obj.NotAssigned == null) { obj.NotAssigned = new List <OrganizationDto>(); } var invited = (from t in db.Organizations join o in db.Users on t.ProtectorID equals o.Id where t.ProtectorID != null && o.Status == StatusEnum.Invited select new OrganizationDto { Id = t.OrganizationID, Name = t.Name }).ToList(); if (invited != null && invited.Any()) { obj.NotAssigned.AddRange(invited); } obj.NotAssigned = obj.NotAssigned.OrderBy(x => x.Name).ToList(); obj.Success = String.Empty; break; default: break; } return(Request.CreateResponse(HttpStatusCode.Created, obj)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } }