Esempio n. 1
0
        public async Task <IActionResult> PutUser(long Id, AlarmUserDTO alarmUserDTO)
        {
            var user = await _context.AlarmUser.Where(user => user.Id.Equals(Id)).FirstAsync();

            if (user == null)
            {
                return(NotFound());
            }

            user.Username = alarmUserDTO.Username;

            user.RFIDTags = _context.RFIDTag.Where(tag => tag.AlarmUserId.Equals(Id)).ToList();

            user.RFIDTags.ToList().ForEach(tag =>
            {
                if (!alarmUserDTO.RFIDTags.Contains(tag.UID))
                {
                    user.RFIDTags.Remove(tag);
                }
            });

            alarmUserDTO.RFIDTags.ForEach(tag =>
            {
                if (!user.RFIDTags.Select(tag => tag.UID).Contains(tag))
                {
                    user.RFIDTags.Add(new RFIDTag
                    {
                        UID = tag
                    });
                }
            });

            _context.Entry(user).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(NoContent());
        }
Esempio n. 2
0
        public async Task <ActionResult <Response <AlarmUser> > > PostUser(AlarmUserDTO alarmUserDTO)
        {
            AlarmUser alarmUser = new AlarmUser
            {
                Username = alarmUserDTO.Username,
                RFIDTags = new List <RFIDTag>()
            };

            alarmUserDTO.RFIDTags.ForEach(tag =>
            {
                alarmUser.RFIDTags.Add(new RFIDTag
                {
                    UID = tag
                });
            });

            _context.AlarmUser.Add(alarmUser);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetUser", new { id = alarmUser.Id }, alarmUser));
        }