Пример #1
0
        public IHttpActionResult CreateNote([FromBody] TradeNoteAddBindingModel Model)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest("invalid data."));
            }
            if (Model == null)
            {
                return(BadRequest("any information about Note was specified"));
            }
            else
            {
                Company Company = TradeNoteContext.Companies.SingleOrDefault(c => c.Id == Model.CompanyId);
                if (Company == null || Company.IsDeleted == true)
                {
                    return(BadRequest("The company with this request doesn't exist"));
                }

                TradeNote NewNote = new TradeNote();
                NewNote.Text      = Model.Text;
                NewNote.CompanyId = Company;

                string          UserId  = User.Identity.GetUserId();
                ApplicationUser AppUser = TradeNoteContext.Users.Single(u => u.Id == UserId);
                NewNote.UserId = AppUser;
                TradeNoteContext.TradeNotes.Add(NewNote);
                TradeNoteContext.SaveChanges();
                return(Ok(NewNote));
            }
        }
Пример #2
0
        // PUT/api/TradeNote/1
        public IHttpActionResult UpdateNote(int Id, [FromBody] TradeNoteUpdateBindingModel Model)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest("invalid data."));
            }
            if (Model == null)
            {
                return(BadRequest("no information about note was specified"));
            }

            TradeNote Note = TradeNoteContext.TradeNotes.SingleOrDefault(c => c.Id == Id);

            if (Note == null)
            {
                return(BadRequest("note with this id does not exist"));
            }
            Company Company = TradeNoteContext.Companies.SingleOrDefault(c => c.Id == Model.CompanyId);

            if (Company.IsDeleted == true)
            {
                Company = null;
            }
            Note.Text      = Model.Text ?? Note.Text;
            Note.CompanyId = Company ?? Note.CompanyId;
            TradeNoteContext.SaveChanges();
            return(Ok());
        }
Пример #3
0
        public IHttpActionResult DeleteTradeNote(int Id)
        {
            TradeNote NoteToDelete = TradeNoteContext.TradeNotes.SingleOrDefault(t => t.Id == Id);

            if (NoteToDelete == null || NoteToDelete.IsDeleted == true)
            {
                return(NotFound());
            }
            TradeNoteContext.TradeNotes.Single(t => t.Id == Id).IsDeleted = true;
            TradeNoteContext.SaveChanges();
            return(Ok());
        }