コード例 #1
0
        public ResponseModel <int> Post([FromBody] GroupModel dataToAdd)
        {
            var resp = new ResponseModel <int>();

            try
            {
                if (dataToAdd == null)
                {
                    throw new Exception("Data is null");
                }

                var model = new Group
                {
                    Name          = dataToAdd.Name,
                    Description   = dataToAdd.Description,
                    IsActive      = dataToAdd.IsActive,
                    IsDelete      = dataToAdd.IsDelete,
                    Date          = dataToAdd.Date,
                    LogoUrl       = dataToAdd.LogoUrl,
                    IdUserCreator = dataToAdd.IdUserCreator
                };

                if (string.IsNullOrEmpty(model?.LogoUrl))
                {
                    model.LogoUrl = string.Empty;
                }

                if (AppRepo.Insert(model))
                {
                    var modelUg = new UserGroup()
                    {
                        IdUser  = model.IdUserCreator,
                        IdGroup = model.Id
                    };

                    if (UserGroupRepo.Insert(modelUg))
                    {
                        resp.Data        = model.Id;
                        resp.Status      = 200;
                        resp.Description = "OK";
                    }
                    else
                    {
                        throw new Exception("No user added to group");
                    }
                }
                else
                {
                    throw new Exception("Not inserted");
                }
            }
            catch (Exception ex)
            {
                resp.Status      = 500;
                resp.Description = $"Error: {ex.Message}";
                resp.Data        = 0;
            }

            return(resp);
        }