Beispiel #1
0
        public async Task <IActionResult> Post([FromBody] HomeDefine homedef)
        {
            if (!ModelState.IsValid)
            {
                HIHAPIUtility.HandleModalStateError(ModelState);
            }

            if (!homedef.IsValid(this._context))
            {
                throw new BadRequestException("Inputted object IsValid Failed");
            }

            // User
            String usrName = String.Empty;

            try
            {
                usrName = HIHAPIUtility.GetUserID(this);
                if (String.IsNullOrEmpty(usrName))
                {
                    throw new UnauthorizedAccessException();
                }
            }
            catch
            {
                throw new UnauthorizedAccessException();
            }

            homedef.Createdby = usrName;
            homedef.CreatedAt = DateTime.Now;
            foreach (var hmem in homedef.HomeMembers)
            {
                hmem.CreatedAt = homedef.CreatedAt;
                hmem.Createdby = usrName;
            }
            _context.HomeDefines.Add(homedef);

            await _context.SaveChangesAsync();

            return(Created(homedef));
        }
Beispiel #2
0
        public async Task <IActionResult> Put([FromODataUri] int key, [FromBody] HomeDefine update)
        {
            if (!ModelState.IsValid)
            {
                HIHAPIUtility.HandleModalStateError(ModelState);
            }

            if (key != update.ID)
            {
                throw new BadRequestException("Inputted ID mismatched");
            }

            // User
            String usrName = String.Empty;

            try
            {
                usrName = HIHAPIUtility.GetUserID(this);
                if (String.IsNullOrEmpty(usrName))
                {
                    throw new UnauthorizedAccessException();
                }
            }
            catch
            {
                throw new UnauthorizedAccessException();
            }

            // Check whether User assigned with specified Home ID
            var hms = _context.HomeMembers.Where(p => p.HomeID == update.ID && p.User == usrName).Count();

            if (hms <= 0)
            {
                throw new UnauthorizedAccessException();
            }

            if (!update.IsValid(this._context))
            {
                throw new BadRequestException("Inputted Object IsValid Failed");
            }

            // Find out the home define
            var existinghd = _context.HomeDefines.Find(key);

            if (existinghd == null)
            {
                throw new NotFoundException("Inputted Object Not Found");
            }
            else
            {
                update.Updatedby = usrName;
                update.UpdatedAt = DateTime.Now;
                update.CreatedAt = existinghd.CreatedAt;
                update.Createdby = existinghd.Createdby;
                _context.Entry(existinghd).CurrentValues.SetValues(update);

                var dbmems = _context.HomeMembers.Where(p => p.HomeID == key).ToList();
                foreach (var mem in update.HomeMembers)
                {
                    var memindb = dbmems.Find(p => p.HomeID == key && p.User == mem.User);
                    if (memindb == null)
                    {
                        mem.Createdby = usrName;
                        mem.CreatedAt = DateTime.Now;
                        _context.HomeMembers.Add(mem);
                    }
                    else
                    {
                        mem.CreatedAt = memindb.CreatedAt;
                        mem.Createdby = memindb.Createdby;
                        _context.Entry(memindb).CurrentValues.SetValues(mem);
                    }
                }
                foreach (var mem in dbmems)
                {
                    var nmem = update.HomeMembers.FirstOrDefault(p => p.User == mem.User);
                    if (nmem == null)
                    {
                        _context.HomeMembers.Remove(mem);
                    }
                }
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException exp)
            {
                if (!_context.HomeDefines.Any(p => p.ID == key))
                {
                    throw new NotFoundException("Inputted Object Not Found");
                }
                else
                {
                    throw new DBOperationException(exp.Message);
                }
            }

            return(Updated(update));
        }