private UnitOfMeasure FindUnitOfMeasure(string uuid, string name, string AccountUUID, ref DataFilter filter)
        {
            UnitOfMeasure res = null;

            if (string.IsNullOrWhiteSpace(uuid) && string.IsNullOrWhiteSpace(name) == false)
            {
                res = this.Search(name, ref filter)?.FirstOrDefault();
            }
            else
            {
                using (var context = new GreenWerxDbContext(this._connectionKey))
                {
                    res = context.GetAll <UnitOfMeasure>(ref filter)?.FirstOrDefault(w => w.UUID == uuid || (w.Name?.EqualsIgnoreCase(name) ?? false));
                }
            }
            if (res == null)
            {
                return(res);
            }

            if (filter.IncludeSystemAccount && res.AccountUUID == AccountUUID)
            {
                return(res);
            }

            if (res.AccountUUID == AccountUUID)
            {
                return(res);
            }

            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Saves user session, expects the authtoken to
        /// not be null or empty. If authtoken is null it uses
        /// the user id instead of ip to generate it.
        /// </summary>
        /// <param name="us"></param>
        /// <returns></returns>
        public UserSession SaveSession(UserSession us)
        {
            if (us == null)
            {
                return(us);
            }

            if (us.SessionLength != SessionLength)
            {
                us.SessionLength = this.SessionLength;
            }

            us.Expires = DateTime.UtcNow.AddMinutes(us.SessionLength);

            if (string.IsNullOrWhiteSpace(us.AuthToken))
            {
                string secret = _app.GetSetting("AppKey")?.Value;
                string issuer = _app.GetSetting("SiteDomain")?.Value;
                User   u      = string.IsNullOrWhiteSpace(us.UserData) ? null : JsonConvert.DeserializeObject <User>(us.UserData);
                us.AuthToken = CreateJwt(secret, u, issuer, us.Expires);
            }

            //make sure other sessions for this user in this account are cleared so there's no confusion.
            this.DeleteByUserId(us.UserUUID, us.AccountUUID, false);

            using (var context = new GreenWerxDbContext(_connectionKey))
            {
                if (context.Insert <UserSession>(us))
                {
                    return(us);
                }
            }
            _logger.InsertError("Failed to save session." + us.UserName, "SessionManager", "SaveSession");
            return(null);
        }
Exemple #3
0
        public ServiceResult Insert(INode n)
        {
            if (!this.DataAccessAuthorized(n, "POST", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            n.Initialize(this._requestingUser.UUID, this._requestingUser.AccountUUID, this._requestingUser.RoleWeight);

            var s = (Strain)n;

            using (var context = new GreenWerxDbContext(this._connectionKey))
            {
                Strain dbU = context.GetAll <Strain>()?.FirstOrDefault(wu => (wu.Name?.EqualsIgnoreCase(s.Name) ?? false) && wu.AccountUUID == s.AccountUUID);

                if (dbU != null)
                {
                    return(ServiceResponse.Error("Strain already exists."));
                }


                if (context.Insert <Strain>((Strain)s))
                {
                    return(ServiceResponse.OK("", s));
                }
            }
            return(ServiceResponse.Error("An error occurred inserting strain " + s.Name));
        }
Exemple #4
0
        /// <summary>
        /// This was created for use in the bulk process..
        ///
        /// </summary>
        /// <param name="p"></param>
        /// <param name="checkName">This will check the products by name to see if they exist already. If it does an error message will be returned.</param>
        /// <returns></returns>
        public ServiceResult Insert(INode n)
        {
            if (!this.DataAccessAuthorized(n, "POST", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            n.Initialize(this._requestingUser.UUID, this._requestingUser.AccountUUID, this._requestingUser.RoleWeight);

            var p = (Currency)n;

            if (string.IsNullOrWhiteSpace(p.CreatedBy))
            {
                return(ServiceResponse.Error("You must assign who the product was created by."));
            }

            if (string.IsNullOrWhiteSpace(p.AccountUUID))
            {
                return(ServiceResponse.Error("The account id is empty."));
            }

            using (var context = new GreenWerxDbContext(this._connectionKey))
            {
                if (context.Insert <Currency>(p))
                {
                    return(ServiceResponse.OK("", p));
                }
            }
            return(ServiceResponse.Error("An error occurred inserting product " + p.Name));
        }
Exemple #5
0
        public ServiceResult Insert(AnatomyTag s)
        {
            using (var context = new GreenWerxDbContext(this._connectionKey))
            {
                AnatomyTag dbU = context.GetAll <AnatomyTag>()?.FirstOrDefault(wu => wu.Name.EqualsIgnoreCase(s.Name) && wu.AccountUUID == s.AccountUUID);

                if (dbU != null)
                {
                    return(ServiceResponse.Error("AnatomyTag already exists."));
                }

                s.UUID     = Guid.NewGuid().ToString("N");
                s.UUIDType = "AnatomyTag";

                if (!this.DataAccessAuthorized(s, "post", false))
                {
                    return(ServiceResponse.Error("You are not authorized this action."));
                }

                if (context.Insert <AnatomyTag>(s))
                {
                    return(ServiceResponse.OK("", s));
                }
            }
            return(ServiceResponse.Error("An error occurred inserting AnatomyTag " + s.Name));
        }
Exemple #6
0
        /// <summary>
        /// set the title if you want it updated on a soft delete, like 'this item as been deleted...'
        /// </summary>
        /// <param name="eventUUID"></param>
        /// <param name="title"></param>
        /// <param name="purge"></param>
        /// <returns></returns>
        public ServiceResult DeleteForEvent(string eventUUID, string title = "", bool purge = false)
        {
            ServiceResult res = ServiceResponse.OK();

            using (var context = new GreenWerxDbContext(this._connectionKey))
            {
                var reminders = context.GetAll <Reminder>().Where(w => w.EventUUID == eventUUID && w.Deleted == false).ToList();

                foreach (var reminder in reminders)
                {
                    if (purge)
                    {
                        if (context.Delete <Reminder>(reminder) == 0)
                        {
                            return(ServiceResponse.Error("Failed to delete reminder."));
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrWhiteSpace(title))
                        {
                            reminder.Name = title;
                        }

                        reminder.Deleted = true;
                        if (context.Update <Reminder>(reminder) == 0)
                        {
                            return(ServiceResponse.Error("Failed to delete reminder."));
                        }
                    }
                }
            }
            return(ServiceResponse.OK());
        }
Exemple #7
0
        public ServiceResult Update(INode n)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("No record sent."));
            }

            var p = (Vendor)n;

            if (!this.DataAccessAuthorized(p, "PATCH", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            ServiceResult res = ServiceResponse.OK();

            using (var context = new GreenWerxDbContext(this._connectionKey))
            {
                if (context.Update <Vendor>(p) == 0)
                {
                    return(ServiceResponse.Error(p.Name + " failed to update. "));
                }
            }
            return(res);
        }
Exemple #8
0
 public IEnumerable <LogEntry> GetAll(DataFilter filter)
 {
     using (var context = new GreenWerxDbContext(_dbConnectionKey))
     {
         return(context.GetAll <LogEntry>(ref filter));
     }
 }
        public List <ProfileMember> GetProfileMembers(string profileUUID, string accountUUID, ref DataFilter filter)
        {
            ///if (!this.DataAccessAuthorized(s, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
            List <ProfileMember> members = new List <ProfileMember>();
            var includeDeleted           = filter == null ? false : filter.IncludeDeleted;
            var includePrivate           = filter == null ? false : filter.IncludePrivate;

            using (var context = new GreenWerxDbContext(this._connectionKey))
            {
                // return context.GetAll<ProfileMember>()?.Where(sw => sw.ProfileUUID == profileUUID &&  sw.AccountUUID == accountUUID)
                //     .OrderBy(ob => ob.SortOrder).ToList();
                DynamicParameters parameters = new DynamicParameters();// see function GetTableData
                parameters.Add("@DELETED", includeDeleted);
                parameters.Add("@PRIVATE", filter?.IncludePrivate);
                parameters.Add("@PROFILEUUID", profileUUID);
                parameters.Add("@ACCOUNTUUID", accountUUID);

                string sql = @"SELECT *
                  FROM [dbo].[ProfileMembers] p
                  WHERE
                    p.ProfileUUID = @PROFILEUUID AND
                    p.AccountUUID= @ACCOUNTUUID AND
	                (p.Private = 0 OR p.Private = @private) AND
	                (p.Deleted = 0 OR p.Deleted = @deleted)"    ;
                members = context.Select <ProfileMember>(sql, parameters).ToList();
            }
            return(members);
        }
Exemple #10
0
 public int Delete(LogEntry l)
 {
     using (var context = new GreenWerxDbContext(_dbConnectionKey))
     {
         return(context.Delete <LogEntry>(l));
     }
 }
Exemple #11
0
 public LogEntry Get(int id)
 {
     using (var context = new GreenWerxDbContext(_dbConnectionKey))
     {
         return(context.Get <LogEntry>(id));
     }
 }
Exemple #12
0
        public async Task <bool> InsertAsync(LogEntry l)
        {
            if (UseFileLogging)
            {
                _fileLog.LogFile(PathToFileLog);
                Exception ex = _fileLog.Write(
                    l.LogDate.ToString() + "," +
                    l.Level + "," +
                    l.StackTrace + "," +
                    l.Source + "," +
                    l.InnerException);

                if (ex == null)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            using (var context = new GreenWerxDbContext(_dbConnectionKey))
            {
                return(await context.InsertAsync <LogEntry>(l) > 0 ? true : false);
            }
        }
Exemple #13
0
 public List <EmailLog> GetEmailLogs(string accountUUID, bool deleted = false)
 {
     using (var context = new GreenWerxDbContext(_dbConnectionKey))
     {
         return(context.GetAll <EmailLog>()?.Where(sw => (sw.AccountUUID == accountUUID) && sw.Deleted == deleted).OrderBy(ob => ob.DateSent).ToList());
     }
 }
Exemple #14
0
        public ServiceResult Delete(INode n)
        {
            ServiceResult res = ServiceResponse.OK();

            if (n == null)
            {
                return(ServiceResponse.Error("No record sent."));
            }

            if (!this.DataAccessAuthorized(n, "DELETE", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            var s = (Tag)n;

            using (var context = new GreenWerxDbContext(this._connectionKey))
            {
                if (context.Delete <Tag>(s) == 0)
                {
                    return(ServiceResponse.Error(s.Value + " failed to delete. "));
                }
            }
            return(ServiceResponse.OK());
        }
Exemple #15
0
        public ServiceResult InsertEventMember(INode n)
        {
            if (!this.DataAccessAuthorized(n, "post", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            n.Initialize(this._requestingUser.UUID, this._requestingUser.AccountUUID, this._requestingUser.RoleWeight);

            var s = (EventMember)n;

            using (var context = new GreenWerxDbContext(this._connectionKey))
            {
                EventMember dbU = context.GetAll <EventMember>()?.
                                  FirstOrDefault(wu => wu.EventUUID == s.EventUUID && wu.UserUUID == s.UserUUID && wu.AccountUUID == s.AccountUUID);

                if (dbU != null)
                {
                    return(ServiceResponse.Error("EventMember already exists."));
                }

                if (context.Insert <EventMember>(s))
                {
                    return(ServiceResponse.OK("", s));
                }
            }
            return(ServiceResponse.Error("An error occurred inserting EventMember " + s.Name));
        }
Exemple #16
0
        public ServiceResult UpdateGroup(INode n)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("Invalid Event group."));
            }

            if (!this.DataAccessAuthorized(n, "PATCH", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            var s = (EventGroup)n;

            s.Name = s.Name.Trim();

            using (var context = new GreenWerxDbContext(this._connectionKey))
            {
                if (context.Update <EventGroup>(s) > 0)
                {
                    return(ServiceResponse.OK());
                }
            }
            return(ServiceResponse.Error("System error, Event was not updated."));
        }
        public ServiceResult Insert(Credential c)
        {
            if (!this.DataAccessAuthorized(c, "POST", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            using (var context = new GreenWerxDbContext(this._connectionKey))
            {
                Credential dbU = context.GetAll <Credential>()?.FirstOrDefault(wu => wu.Name.EqualsIgnoreCase(c.Name) && wu.AccountUUID == c.AccountUUID);

                if (dbU != null)
                {
                    return(ServiceResponse.Error("Credential already exists."));
                }

                c.UUID = Guid.NewGuid().ToString("N");

                c.UUIDType = "Credential";
                if (context.Insert <Credential>(c))
                {
                    return(ServiceResponse.OK("", c));
                }
            }
            return(ServiceResponse.Error("An error occurred inserting credential " + c.Name));
        }
Exemple #18
0
        /// <summary>
        /// We pass in the eventUUID because the client allows to pick from previous event locations
        /// If we try to save a new event based on a previouse location the eventlocation.evenuuid will
        /// be changed in the table by the update and the other event will lose its location. So we need
        /// a unique eventUUID and UUID combo
        /// </summary>
        /// <param name="uuid"></param>
        /// <param name="eventUUID"></param>
        /// <returns></returns>
        public ServiceResult GetEventLocation(string uuid, string eventUUID)
        {
            //Func<EventLocation, Location, EventLocation> UpdateLocation
            //                                                = ((a, b) => {
            //                                                    a.Latitude = b.Latitude;
            //                                                    a.Longitude = b.Longitude;
            //                                                    return a; });
            try
            {
                using (var context = new GreenWerxDbContext(this._connectionKey))
                {
                    var location = context.GetAll <EventLocation>()?.FirstOrDefault(sw => sw.UUID == uuid && sw.EventUUID == eventUUID);
                    //.Join(context.GetAll<Location>(),
                    // evtLocation => evtLocation.LocationUUID,
                    // tmpLoc => tmpLoc.UUID,
                    // (evtLocation, tmpLoc) => new { evtLocation, tmpLoc })
                    // .Select(s => UpdateLocation(s.evtLocation, s.tmpLoc))?.FirstOrDefault();

                    if (location == null)
                    {
                        return(ServiceResponse.Error("Location was not found."));
                    }

                    return(ServiceResponse.OK("", location));
                }
                //////if (!this.DataAccessAuthorized(s, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
            }
            catch (Exception ex)
            {
                Debug.Assert(false, ex.Message);
                return(ServiceResponse.Error("Error loading location"));
            }
        }
Exemple #19
0
 public List <PriceRuleLog> GetPriceRules(string trackingId, string trackingType, bool isDeleted = false)
 {
     using (var context = new GreenWerxDbContext(this._connectionKey))
     {
         return(context.GetAll <PriceRuleLog>()?.Where(sw => (sw.TrackingId == trackingId && (sw.TrackingType?.EqualsIgnoreCase(trackingType) ?? false)) && sw.Deleted == isDeleted).OrderBy(ob => ob.Name).ToList());
     }
 }
Exemple #20
0
        public ServiceResult SearchEventLocation(string name, bool includePartialMatch)
        {
            try
            {
                name = name.ToLower();
                using (var context = new GreenWerxDbContext(this._connectionKey))
                {
                    List <EventLocation> locations = new List <EventLocation>();
                    if (includePartialMatch)
                    {
                        locations = context.GetAll <EventLocation>()
                                    .Where(sw => sw.Name.ToLower() == name ||
                                           sw.Name.ToLower().Contains(name)).ToList();
                    }
                    else
                    {
                        locations = context.GetAll <EventLocation>().Where(sw => sw.Name.ToLower() == name.ToLower()).ToList();
                    }
                    if (locations == null)
                    {
                        return(ServiceResponse.Error("Location was not found."));
                    }

                    return(ServiceResponse.OK("", locations));
                }
                //////if (!this.DataAccessAuthorized(s, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
            }
            catch (Exception ex)
            {
                Debug.Assert(false, ex.Message);
                return(ServiceResponse.Error("Error loading location"));
            }
        }
Exemple #21
0
        public ServiceResult Delete(string type, string uuid, string accountUUID, bool purge = false)
        {
            ServiceResult res = ServiceResponse.OK();

            INode n = this.GetItem(type, uuid, accountUUID);

            if (n == null)
            {
                return(ServiceResponse.Error("Item not found."));
            }

            if (!this.DataAccessAuthorized(n, "DELETE", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            using (var context = new GreenWerxDbContext(this._connectionKey))
            {
                if (purge && context.Delete(n) == 0)
                {
                    return(ServiceResponse.Error(n.Name + " failed to delete. "));
                }

                n.Deleted = true;
                if (context.Update(n) == 0)
                {
                    return(ServiceResponse.Error(n.Name + " failed to delete. "));
                }
            }
            return(res);
        }
Exemple #22
0
        public List <User> GetEventMembers(string eventUUID, string accountUUID)
        {
            List <User> members = new List <User>();

            try
            {
                using (var context = new GreenWerxDbContext(this._connectionKey))
                {
                    members = context.GetAll <EventMember>()?.Where(w => w.EventUUID == eventUUID)
                              .Join(
                        context.GetAll <User>()
                        .Where(w => w.Deleted == false &&
                               w.AccountUUID == accountUUID),
                        acct => acct.UserUUID,
                        users => users.UUID,
                        (acct, users) => new { acct, users }
                        )
                              .Select(s => s.users)
                              .ToList();
                }
                if (members == null)
                {
                    return(new List <User>());
                }

                members = UserManager.ClearSensitiveData(members);
                ///if (!this.DataAccessAuthorized(u, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
            }
            catch (Exception ex)
            {
                Debug.Assert(false, ex.Message);
            }
            return(members);
        }
Exemple #23
0
        public List <dynamic> GetAll(string type = "")
        {
            List <dynamic> res = new List <dynamic>();

            if (string.IsNullOrWhiteSpace(type))
            {
                return(res);
            }

            if (this._requestingUser == null || string.IsNullOrWhiteSpace(this._requestingUser.AccountUUID))
            {
                return(res);
            }

            if (type == "all")
            {
                using (var context = new GreenWerxDbContext(this._connectionKey))
                {
                    res.AddRange(context.GetAll <GreenWerx.Models.Plant.Plant>()?.Where(w => w.AccountUUID == this._requestingUser.AccountUUID).Cast <dynamic>().ToList());
                    res.AddRange(context.GetAll <Ballast>()?.Where(w => w.AccountUUID == this._requestingUser.AccountUUID).Cast <dynamic>().ToList());
                    res.AddRange(context.GetAll <Bulb>()?.Where(w => w.AccountUUID == this._requestingUser.AccountUUID).Cast <dynamic>().ToList());
                    res.AddRange(context.GetAll <Fan>()?.Where(w => w.AccountUUID == this._requestingUser.AccountUUID).Cast <dynamic>().ToList());
                    res.AddRange(context.GetAll <Filter>()?.Where(w => w.AccountUUID == this._requestingUser.AccountUUID).Cast <dynamic>().ToList());
                    res.AddRange(context.GetAll <Pump>()?.Where(w => w.AccountUUID == this._requestingUser.AccountUUID).Cast <dynamic>().ToList());
                    res.AddRange(context.GetAll <Vehicle>()?.Where(w => w.AccountUUID == this._requestingUser.AccountUUID).Cast <dynamic>().ToList());
                }

                return(res);
            }

            return(this.GetEquipment(type)?.Where(w => w.AccountUUID == this._requestingUser.AccountUUID).ToList());
        }
Exemple #24
0
        public List <dynamic> GetFavoriteEvents(string userUUID, string accountUUID)
        {
            try
            {
                using (var context = new GreenWerxDbContext(this._connectionKey))
                {
                    if (context.GetAll <Favorite>()?.FirstOrDefault() == null)
                    {
                        return(new List <dynamic>());
                    }

                    var events = context.GetAll <Favorite>()?.Where(w => w?.CreatedBy == userUUID && w?.AccountUUID == accountUUID &&
                                                                    w.UUIDType.EqualsIgnoreCase("event"))
                                 ?.Join(context.GetAll <Event>(),
                                        rem => rem.ItemUUID,
                                        evt => evt.UUID,
                                        (rem, evt) => new { rem, evt })
                                 ?.Select(s => new Favorite()
                    {
                        UUID        = s.rem.UUID,
                        UUIDType    = s.rem.UUIDType,
                        CreatedBy   = s.rem.CreatedBy,
                        AccountUUID = s.rem.AccountUUID
                    });

                    return(events.Cast <dynamic>().ToList());
                }
                //////if (!this.DataAccessAuthorized(s, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
            }
            catch (Exception ex)
            {
                Debug.Assert(false, ex.Message);
            }
            return(new List <dynamic>());
        }
Exemple #25
0
        public int Delete(AnatomyTag s, bool purge = false)
        {
            if (s == null)
            {
                return(0);
            }

            if (!this.DataAccessAuthorized(s, "DELETE", false))
            {
                return(0);
            }

            if (purge)
            {
                using (var context = new GreenWerxDbContext(this._connectionKey))
                {
                    return(context.Delete <AnatomyTag>(s));
                }
            }

            //get the AnatomyTag from the table with all the data so when its updated it still contains the same data.
            s = this.GetAnatomyTagBy(s.UUID);
            if (s == null)
            {
                return(0);
            }
            s.Deleted = true;
            using (var context = new GreenWerxDbContext(this._connectionKey))
            {
                return(context.Update <AnatomyTag>(s));
            }
        }
Exemple #26
0
        public List <Event> GetPrivateSubEvents(string parentUUID, string userUUID, string accountUUID, bool includeParent = false, bool deleted = false)
        {
            try
            {
                using (var context = new GreenWerxDbContext(this._connectionKey))
                {
                    if (includeParent)
                    {
                        return(context.GetAll <Event>()?.Where(sw =>
                                                               (sw.UUID == parentUUID || sw.UUParentID == parentUUID) &&
                                                               (sw.Private == true && sw.CreatedBy == userUUID && sw.AccountUUID == accountUUID) &&
                                                               sw?.Deleted == deleted).OrderBy(ob => ob?.StartDate)?.ToList());
                    }

                    return(context.GetAll <Event>()?.Where(sw =>
                                                           sw.UUParentID == parentUUID &&
                                                           (sw.Private == true && sw.CreatedBy == userUUID && sw.AccountUUID == accountUUID) &&
                                                           sw?.Deleted == deleted).OrderBy(ob => ob?.StartDate)?.ToList());
                }
                //////if (!this.DataAccessAuthorized(s, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
            }
            catch (Exception ex)
            {
                Debug.Assert(false, ex.Message);
            }
            return(new List <Event>());
        }
Exemple #27
0
        private List <Strain> FindStrain(string uuid, string name, string AccountUUID, bool includeDefaultAccount = true)
        {
            List <Strain> res = null;

            if (string.IsNullOrWhiteSpace(uuid) && string.IsNullOrWhiteSpace(name) == false)
            {
                res = this.Search(name);
            }
            else
            {
                using (var context = new GreenWerxDbContext(this._connectionKey))
                {
                    res = context.GetAll <Strain>()?.Where(w => w.UUID == uuid || (w.Name?.EqualsIgnoreCase(name) ?? false)).ToList();
                }
            }

            if (res == null)
            {
                return(res);
            }

            if (includeDefaultAccount && (res.FirstOrDefault().AccountUUID == AccountUUID))
            {
                return(res);
            }

            if (res.FirstOrDefault().AccountUUID == AccountUUID)
            {
                return(res);
            }

            return(new List <Strain>());
        }
Exemple #28
0
        public ServiceResult Insert(INode n)
        {
            if (!this.DataAccessAuthorized(n, "post", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            n.Initialize(this._requestingUser.UUID, this._requestingUser.AccountUUID, this._requestingUser.RoleWeight);

            var s = (Event)n;

            try
            {
                using (var context = new GreenWerxDbContext(this._connectionKey))
                {
                    s.Name = s.Name.Trim();
                    if (context.Insert <Event>(s))
                    {
                        return(ServiceResponse.OK("", s));
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Assert(false, ex.Message);
            }
            return(ServiceResponse.Error("An error occurred inserting Event " + s.Name));
        }
Exemple #29
0
        public ServiceResult Delete(INode n, bool purge = false)
        {
            ServiceResult res = ServiceResponse.OK();

            if (n == null)
            {
                return(ServiceResponse.Error("No record sent."));
            }

            if (!this.DataAccessAuthorized(n, "DELETE", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            using (var context = new GreenWerxDbContext(this._connectionKey))
            {
                if (purge && context.Delete <StageData>((StageData)n) == 0)
                {
                    return(ServiceResponse.Error(n.Name + " failed to delete. "));
                }
                //get the stage data from the table with all the data so when its updated it still contains the same data.
                res = this.Get(n.UUID);
                if (res.Code != 200)
                {
                    return(res);
                }

                n.Deleted = true;
                if (context.Update <StageData>((StageData)n) == 0)
                {
                    return(ServiceResponse.Error(n.Name + " failed to delete. "));
                }
            }
            return(res);
        }
        public ServiceResult Insert(INode n)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("Value is empty."));
            }

            n.Initialize(this._requestingUser.UUID, this._requestingUser.AccountUUID, this._requestingUser.RoleWeight);

            var s = (UnitOfMeasure)n;

            using (var context = new GreenWerxDbContext(this._connectionKey))
            {
                UnitOfMeasure dbU = context.GetAll <UnitOfMeasure>()?.FirstOrDefault(wu => (wu.Name?.EqualsIgnoreCase(s.Name) ?? false) && wu.AccountUUID == s.AccountUUID);

                if (dbU != null)
                {
                    return(ServiceResponse.Error("UnitOfMeasure already exists."));
                }

                if (context.Insert <UnitOfMeasure>(s))
                {
                    return(ServiceResponse.OK("", s));
                }
            }
            return(ServiceResponse.Error("An error occurred inserting UnitOfMeasure " + s.Name));
        }