public GroupResultModel CreateGroup(CreateGroupRequestModel param) { GroupResultModel result = new GroupResultModel(); try { int?memberId = IdentityHelper.GetMemberId(); if (!memberId.HasValue) { throw new OrgException("Invalid MemberId"); } GroupBL bl = new GroupBL(); var group = bl.CreateGroup(memberId.Value, param, OrgComm.Data.Models.Group.GroupType.Other); result.Status = true; result.Message = "Group created"; result.Group = group; } catch (OrgException oex) { result.Status = false; result.Message = oex.Message; } catch (Exception ex) { result.Status = false; result.Message = AppConfigs.InternalErrorMessage; if (AppConfigs.DebugInternalMessage) { result.InternalMessage = ex.Message; } } return(result); }
public GroupModel CreateGroup(int memberId, CreateGroupRequestModel model, OrgComm.Data.Models.Group.GroupType groupType) { GroupModel groupModel = null; OrgComm.Data.Models.Group group = new OrgComm.Data.Models.Group { FounderId = memberId, Type = (int)groupType, Title = model.Title, SubTitle = model.SubTitle, WelcomeMessage = model.WelcomeMessage, CreatedDate = DateTime.Now }; if (model.Logo != null) { byte[] logo = model.Logo.Buffer; using (System.IO.MemoryStream msReader = new System.IO.MemoryStream(logo)) { using (System.Drawing.Image img = System.Drawing.Image.FromStream(msReader)) { string imageType = ImageHelper.GetImageFormat(img); if (imageType == null) { throw new OrgException("Not support image type"); } int? width, height; Image imgResize = null; //Size Max constraint width = AppConfigs.GroupPhotoWidthMax; height = AppConfigs.GroupPhotoHeightMax; imgResize = ImageHelper.ReSize(img, width, height, ImageHelper.ResizeMode.KeepAspectRatio); group.Logo = ImageHelper.ImageToByteArray(imgResize, img.RawFormat); } } } using (OrgCommEntities dbc = new OrgCommEntities(DBConfigs.OrgCommConnectionString)) { if (groupType == OrgComm.Data.Models.Group.GroupType.Other) { if (!dbc.Members.Any(r => (r.Id.Equals(group.FounderId)))) { throw new OrgException("Invalid member"); } } else if (groupType == OrgComm.Data.Models.Group.GroupType.Company) { if (!dbc.Company.Any(r => (r.Id.Equals(group.FounderId)))) { throw new OrgException("Invalid Company"); } } dbc.Groups.Add(group); dbc.SaveChanges(); var lookupGroupType = dbc.Lookups.SingleOrDefault(r => (r.TypeId == (int)OrgComm.Data.Models.Lookup.LookupType.GroupType) && (r.Value == (int)groupType)); var lookupJoinedStatus = dbc.Lookups.SingleOrDefault(r => (r.TypeId == (int)OrgComm.Data.Models.Lookup.LookupType.GroupMemberJoinedStatus) && (r.Value == (int)OrgComm.Data.Models.GroupMember.JoinedStatusType.Active)); groupModel = new GroupModel { Id = group.Id, FounderId = group.FounderId, Type = group.Type, TypeDescription = (lookupGroupType == null) ? null : lookupGroupType.Description, Title = group.Title, SubTitle = group.SubTitle, WelcomeMessage = group.WelcomeMessage, Logo = (group.Logo == null) ? null : String.Format(GroupBL.LogoUrlFormatString, group.Id), Status = (int)OrgComm.Data.Models.GroupMember.JoinedStatusType.Active, StatusDescription = (lookupJoinedStatus == null) ? null : lookupJoinedStatus.Description, RoomId = ChatBL.GetChatRoomId(new int[] { group.Id }, ChatBL.ParticipationType.Group), }; if (groupType == OrgComm.Data.Models.Group.GroupType.Other) { try { var groupmember = new OrgComm.Data.Models.GroupMember { GroupId = group.Id, MemberId = group.FounderId, JoinedDate = group.CreatedDate, JoinedStatus = (int)OrgComm.Data.Models.GroupMember.JoinedStatusType.Active }; dbc.GroupMembers.Add(groupmember); dbc.SaveChanges(); } catch (System.Exception ex) { throw new OrgException("Cannot member to group", ex); } groupModel.Members = GroupBL.GetGroupMember(dbc, group.Id); } } return(groupModel); }