Content Record

Topics: Core, Customizing Orchard, General, Troubleshooting
Dec 3, 2012 at 2:33 PM

I am building a module, which will have an apiController with 2 functions:

get userid - return a list of ints (list of favorite pages ids)

get user id + page id - save to DB

 

I created this record:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement.Records;

namespace YIT.FavoriteArticles.Models
{
    public class UserFavoriteArticleRecord : ContentPartRecord
    {
        public virtual int userId { get; set; }
        public virtual int articleId { get; set; }

    }
}

My problem is that in order to create a migration i need this record to be a content part,

which means that in order to create instances of this record, i need to associate the content parts to a page.

How can I build this controller with a migration or just connect it to a table I created in the database without the record instances appearing in the admin gui?

 

Thanks :)

Coordinator
Dec 3, 2012 at 7:27 PM

Why did you derive from ContentPartRecord? All you need is an Id column.

Dec 4, 2012 at 8:24 AM
Edited Dec 4, 2012 at 8:30 AM

if I don't derive from contentpartRecord, AND create a migration with codegen,

how would a be able to create record instances (and save in DB) with my apiController?

And how would I be able to get records based on the userId?

Coordinator
Dec 5, 2012 at 7:19 AM

If you have an Id properly defined, it will get mapped, and you can inject an IRepository<T> to manipulate the table.

Dec 5, 2012 at 10:25 AM
Edited Dec 5, 2012 at 2:05 PM

When I use the Create method of Irepository, do I need to give an id to the record I'm passing it?

or does it generate the id?

for example, is this good enough?

 

  public HttpResponseMessage AttachArticleToUser(int userId, int articleId)
        {
            try
            {
                UserFavoriteArticleRecord record = new UserFavoriteArticleRecord();
                record.articleId = articleId;
                record.userId = userId;

                _repository.Create(record);

                return Request.CreateResponse(HttpStatusCode.OK, "1");
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.NotAcceptable, ex.Message);
            }
        }

 

And another question, do I need to add something to the migration automatically created for the module by code genartion?

Coordinator
Dec 5, 2012 at 7:10 PM

It does generate it.

Your migration should set-up the Id column something like this: Column<int>("Id", column => column.PrimaryKey().NotNull());