示例#1
0
        public async Task <IActionResult> AddRecord([Bind("Record_Name,Root,Type,Notes_And_Chords")] Record record, string Tags)
        {
            if (record.Record_Name != null && record.Notes_And_Chords != null)
            {
                record.Length = record.Notes_And_Chords.Split(',').Count(); //calculated field based on length of notes and chords
                if (ModelState.IsValid)
                {
                    _context.Add(record);
                    await _context.SaveChangesAsync();
                }

                var fName = User.Identity.Name.Split(' ')[0];
                _user = _context.Users.Where(u => u.First_Name == fName).First();
                var    foundRec = _context.Records.Find(record.Record_ID);
                RecTag rT       = new RecTag
                {
                    Owner_ID = _user.User_ID,
                    Rec_ID   = foundRec.Record_ID,
                    Tags     = Tags
                };

                _context.Add(rT);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", "Admin"));
            }
            else
            {
                //return error idf issues with add
                return(RedirectToAction("Index", "Admin", new { error = "Invalid Record Details" }));
            }
        }
        /*
         * Adds new tags to record, if rec already had a tag, it is added
         */
        public async void AddTag(string tags, string record)
        {
            string fName = User.Identity.Name.Split(' ')[0];
            var    user  = _context.Users.Where(u => u.First_Name == fName).FirstOrDefault();
            var    rec   = _context.Records.Where(r => r.Record_Name == record).FirstOrDefault();

            //if tag already exists, replace original tags
            if (_context.RecTags.Any(rt => rt.Owner_ID == user.User_ID && rt.Rec_ID == rec.Record_ID))
            {
                var existingtags = _context.RecTags.Where(rt => rt.Owner_ID == user.User_ID && rt.Rec_ID == rec.Record_ID).First();
                existingtags.Tags = tags;
                _context.RecTags.Update(existingtags);
            }
            else
            {
                var newTags = new RecTag()
                {
                    Owner_ID = user.User_ID,
                    Rec_ID   = rec.Record_ID,
                    Tags     = tags
                };
                _context.RecTags.Add(newTags);
            }
            await _context.SaveChangesAsync();
        }