public JsonResult AddGroup(MyPageGroupNewViewModel.AddGroupModel addGroupModel) { long loginMemberId = GetLoginMemberId(); var group_name = addGroupModel.GroupName; try { ///////////////////////////////////////////////////////// //入力チェック if (loginMemberId <= 0 && String.IsNullOrEmpty(group_name) ) { addGroupModel.ErrorMessage = "グループを作成できません。"; return Json(addGroupModel, JsonRequestBehavior.AllowGet); } var group = (from g in com.Groups where g.GroupName == group_name && g.MemberID == loginMemberId && g.Status == true select g).FirstOrDefault(); if (group != null) { addGroupModel.ErrorMessage = "同じ名前のグループがすでにあります。"; return Json(addGroupModel, JsonRequestBehavior.AllowGet); } ///////////////////////////////////////////////////////// //新規グループを作成する var newGroup = new Groups { GroupName = group_name, MemberID = loginMemberId, Status = true, // 0・・無効 1・・有効 CreatedAccountID = loginMemberId.ToString() }; com.Groups.Add(newGroup); com.SaveChanges(); var group_id = (from g in com.Groups where g.GroupName == group_name && g.MemberID == loginMemberId select g.GroupID).FirstOrDefault(); addGroupModel.GroupID = Convert.ToInt32(group_id); ///////////////////////////////////////////////////////// //グループメンバーの更新 using (var dbContextTransaction = com.Database.BeginTransaction()) { try { if (addGroupModel.MemberIDs == null) { addGroupModel.MemberIDs = new List<long>(); } //メンバーに自身を追加 addGroupModel.MemberIDs.Add(loginMemberId); //通知メールのタイトルと本文を取得 NoticeInfo noticeInfo = (from ni in com.NoticeInfo where ni.MailDelivCondID == 4 && ni.NoticeClass == 3 select ni).FirstOrDefault(); string title = noticeInfo.Title; string body = noticeInfo.Body; //追加メンバーの一覧からグループメンバーを新規作成 foreach (var newMemberId in addGroupModel.MemberIDs) { var newGroupMember = new GroupMember { GroupID = group_id, MemberID = newMemberId, CreatedAccountID = loginMemberId.ToString(), CreatedDate = DateTime.Now }; com.GroupMember.Add(newGroupMember); //新規に追加する会員向けにメール通知 if (loginMemberId == newMemberId) continue; //追加する会員向けにメール通知 NoticeInfoForMyPage notice = new NoticeInfoForMyPage(); notice.ClassClass = NoticeInfoForMyPage.CLS_GROUP; notice.AddGroup = (from gm in com.Member where gm.MemberId == loginMemberId select gm.Nickname).FirstOrDefault(); notice.GroupID = group_id.ToString(); notice.Group = (from g in com.Groups where g.GroupID == group_id select g.GroupName).FirstOrDefault(); notice.Nickname = (from gm in com.Member where gm.MemberId == loginMemberId select gm.Nickname).FirstOrDefault(); notice.setTitle(NoticeInfoForMyPage.CLS_GROUP, title); notice.setBody(NoticeInfoForMyPage.CLS_GROUP, body); MyPageCommon.SendMail(newMemberId, notice.Title, notice.Body); //新規に追加する会員向けにお知らせ配信対象を生成 var newNotice = new NoticeDeliverySubject { NoticeId = Convert.ToInt32(noticeInfo.NoticeId), MemberId = Convert.ToInt32(newMemberId), ClassClass = 7, UniqueID = Convert.ToInt32(loginMemberId), //グループに入れた会員IDを保存 UniqueID2 = Convert.ToInt32(group_id), AlreadyReadFlg = false, NoticeDeliveryStatus = 1, CreatedAccountID = loginMemberId.ToString(), CreatedDate = DateTime.Now }; com.NoticeDeliverySubject.Add(newNotice); } com.SaveChanges(); dbContextTransaction.Commit(); } catch (Exception) { dbContextTransaction.Rollback(); } } } catch (Exception ex) { throw ex; } return Json(addGroupModel, JsonRequestBehavior.AllowGet); }
public JsonResult UpdateGroup(MyPageGroupEditViewModel.AddGroupModel addGroupModel) { long loginMemberId = GetMemberID(); var group_id = addGroupModel.GroupID; try { ///////////////////////////////////////////////////////// //入力チェック if (loginMemberId <= 0 && group_id <= 0) { addGroupModel.ErrorMessage = "グループを編集できません。"; return Json(addGroupModel, JsonRequestBehavior.AllowGet); } var group = (from g in com.Groups where g.GroupID == group_id select g).FirstOrDefault(); if (group == null) { addGroupModel.ErrorMessage = "グループがありません。"; return Json(addGroupModel, JsonRequestBehavior.AllowGet); } ///////////////////////////////////////////////////////// //グループ情報を更新する group.GroupName = addGroupModel.GroupName; group.ModifiedAccountID = loginMemberId.ToString(); group.ModifiedDate = DateTime.Now; ///////////////////////////////////////////////////////// //グループメンバーの更新 using (var dbContextTransaction = com.Database.BeginTransaction()) { try { //既存メンバーを一旦削除 //TODO 将来削除対象のみ削除するように変更 var existingMembers = from g in com.GroupMember where g.GroupID == group_id select g; foreach (var gm in existingMembers) { com.GroupMember.Remove(gm); } com.SaveChanges(); //メンバーに自身を追加 if (addGroupModel.MemberIDs == null) addGroupModel.MemberIDs = new List<long> { }; if (!addGroupModel.MemberIDs.Contains(loginMemberId)) addGroupModel.MemberIDs.Add(loginMemberId); //通知メールのタイトルと本文を取得 NoticeInfo noticeInfo = (from ni in com.NoticeInfo where ni.MailDelivCondID == 4 && ni.NoticeClass == 3 select ni).FirstOrDefault(); string title = noticeInfo.Title; string body = noticeInfo.Body; //追加メンバーの一覧からグループメンバーを新規追加 //TODO 将来新規追加対象のみ追加するように変更 foreach (long newMemberId in addGroupModel.MemberIDs) { var newGroupMember = new GroupMember { GroupID = group_id, MemberID = newMemberId, CreatedAccountID = loginMemberId.ToString(), CreatedDate = DateTime.Now }; com.GroupMember.Add(newGroupMember); //新規に追加する会員向けにメール通知 if (!IsNewMember(newMemberId, loginMemberId, existingMembers)) continue; NoticeInfoForMyPage notice = new NoticeInfoForMyPage(); notice.ClassClass = NoticeInfoForMyPage.CLS_GROUP; notice.AddGroup = (from gm in com.Member where gm.MemberId == loginMemberId select gm.Nickname).FirstOrDefault(); notice.GroupID = group_id.ToString(); notice.Group = (from g in com.Groups where g.GroupID == group_id select g.GroupName).FirstOrDefault(); notice.Nickname = (from gm in com.Member where gm.MemberId == loginMemberId select gm.Nickname).FirstOrDefault(); notice.setTitle(NoticeInfoForMyPage.CLS_GROUP, title); notice.setBody(NoticeInfoForMyPage.CLS_GROUP, body); MyPageCommon.SendMail(newMemberId, notice.Title, notice.Body); //新規に追加する会員向けにお知らせ配信対象を生成 var newNotice = new NoticeDeliverySubject { NoticeId = Convert.ToInt32(noticeInfo.NoticeId), MemberId = Convert.ToInt32(newMemberId), ClassClass = 7, UniqueID = Convert.ToInt32(loginMemberId), //グループに入れた会員IDを保存 UniqueID2 = Convert.ToInt32(group_id), AlreadyReadFlg = false, NoticeDeliveryStatus = 1, CreatedAccountID = loginMemberId.ToString(), CreatedDate = DateTime.Now }; com.NoticeDeliverySubject.Add(newNotice); } com.SaveChanges(); dbContextTransaction.Commit(); } catch (Exception) { dbContextTransaction.Rollback(); } } } catch (Exception ex) { throw ex; } return Json(addGroupModel, JsonRequestBehavior.AllowGet); }